//https://[Log in to view URL]

// PROGRAM 3
#include <stdio.h>
int main(void)
{
    int arr[] = {10, 20};
    char a[]="hello";
    int *p = arr;
    *++p;//*(++p)//so increments the next values
    printf("arr[0] = %d, arr[1] = %d, *p = %d",
                          arr[0], arr[1], *p);
    char *c = a;
    *++c;
    printf("\na[0] = %c, a[1] = %c, *c = %c",
                          a[0], a[1], *c);
    return 0;
}//arr[0] = 10, arr[1] = 20, *p = 20

#if 0
// PROGRAM 2
#include <stdio.h>
int main(void)
{
    int arr[] = {10, 20};
    int *p = arr;
    *p++;//*(p++)//so increments the next values
    printf("arr[0] = %d, arr[1] = %d, *p = %d",
                          arr[0], arr[1], *p);
    return 0;
}//arr[0] = 10, arr[1] = 20, *p = 20


// PROGRAM 1//same as post and pre increment
#include <stdio.h>
int main(void)
{
    int arr[] = {10, 20};
    int *p = arr;
    ++*p;//++(*p)//so increments the value directly
    printf("arr[0] = %d, arr[1] = %d, *p = %d",
                          arr[0], arr[1], *p);
    return 0;
}//arr[0] = 11, arr[1] = 20, *p = 11
#endif

/*
The output of the above programs and all such programs can be easily guessed
by remembering following simple rules about postfix ++, prefix ++, and 
* (dereference) operators 

1) Precedence of prefix ++ and * is same. Associativity of both is right to left.

2) Precedence of postfix ++ is higher than both * and prefix ++.
Associativity of postfix ++ is left to right.(Refer: Precedence Table)

The expression ++*p has two operators of same precedence, so compiler looks for
associativity. Associativity of operators is right to left.
Therefore the expression is treated as ++(*p). Therefore the output of first
program is “arr[0] = 11, arr[1] = 20, *p = 11“.

The expression *p++ is treated as *(p++) as the precedence of postfix ++ is
higher than *. Therefore the output of second program is “arr[0] = 10, arr[1] = 20,
*p = 20 “.

The expression *++p has two operators of same precedence, so compiler looks for
associativity. Associativity of operators is right to left. Therefore the 
expression is treated as *(++p). Therefore the output of third program is 
“arr[0] = 10, arr[1] = 20, *p = 20“.

*/

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: