c - Postfix before prefix? -


i have read in here , in here postfix(es) prior prefix(es).

int = 5; int b = 5; printf("%d\n",a++); printf("%d\n",++b); 

but code output 5,6. how make sense then?

what talked in links operator precedence. not affect working of post increment. post increment operator increases value after expression in calculated.

post-increment operator used increment value of variable after executing expression in post increment used.

what means if have statement

int = 0 , j = 5 , k ; k = ++i + j++ ; 

the ++i calculated ( i becomes 1 ) , expression calculated, , k gets value 6 , , after assigning value 6 k, effect of j++ comes place , j becomes 6.

operator precedence determines how operators grouped, when different operators appear close in 1 expression. example, ' * ' has higher precedence ' + '. thus, expression + b * c means multiply b , c , , add product (i.e., + (b * c) ).

but precedence not change working of postfix increment. increase value after expression calculated ( part independent of it's precedence ) .

i'll give simple example ( hope know using pointers )

#include<stdio.h> int main()   {     int a[] = { 11, 22 };     int x;     int *p = a;     x = *p++;     printf( " *p = %d\n",*p );     printf( " x = %d",x );   } 

the output is

 *p = 22  x = 11 

you can refer ideone link proof.

now lets explain that. ++ has higher precedence * , code same as

x = * ( p++ ); 

that is, ++ will make pointer p point next address of array , part done after expression calculated ( in other words, after value of *p assigned x ) . after expression, p point next address, have value 22 while x still value 11.

hope makes clear ( example might bit hard understand, it's 1 of best understand )


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -