Code: int main() { int a=7, t=0; t=--a+--a+a+++a; printf("%d",t); } output: 20 I could not understand why it prints '20' ? please explain it !
I'd suspect it's undefined behavior because you're trying to modify a more than once in a sequence. Code: int a[] = { 0, 1, 2, 3 }, b[] = { 4, 5, 6, 7 }, i=0; a[i++] = b[++i]; the compiler can't tell which operation it should do first - increment i in a or i in b or assign then increment, etc. The same thing occurs in your snippit.
Well I can do that... Inititally a was 7. Such expressions are explained in the order : 1. Pre 2. Expression 3. Post and while solving the expression, a variable has all the same values... expression is: --a + --a + a++ + a initially a is 7 1. All pre operations are done... name --a & --a. therefore, a becomes 5. 2. Expression is solved with all the a's as 5... Giving you 5 + 5 + 5 + 5 = 20; 3. Post operations are a++... giving a = 6. Hope this helps.
you are very right HARIT, See this page's NOTE 2 difranco.net/compsci/C_Operator_Precedence_Table.htm it states that : Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.
you are very right HARIT, but Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.
I change the example a little. Now the o/p is j=2 *p=5 here the statement act like j= --*p and p++. why? if the actual increment or decrement of the operand is delayed, then the statement should be act like j=(--*p)++. WHY POST INCREMENT OPERATOR INCREMENT THE ADDRESS HOLED BY P, NOT THE VALUE OF P??
Code: int main() { int a=7, t=0; t=(--a)+(--a)+a+(++a); printf("%d\n",t); printf("%d",a); return 0; } This code replies 21 instead of 20 because here a++ statement is replaced by ++a(notice the last parenthisis),here actually ( ++a) specifying the we are performing preincrementation,but in the problem that you specified since postincremented order >preincremented therefore answer comes out to be 20.
Sorry friend, in TurboC output of this code is 24 6 Here the simple reason, parenthesis work first and value of a incremented by 1 then decremented twice. so now the value of 'a' is 6 and the statement will look like t=6+6+6+6, that returns 24.
a= 10; printf("a a++ a a++ a a++ a ++a a \ %d %d %d %d %d %d %d %d %d\n" \ , a, a++ ,a ,a++ ,a, a++ , a, ++a, a); PLEASE explain precedence within printf different combination gives different answers always..
Sorry Friend!! In gcc compiler the output is 21. Code: #include<stdio.h> void main() { int a=7, t=0; t=--a+--a+a+++a; printf("%d\n",t); } ar@ar-V:~$ gcc x.c ar@ar-V:~$ ./a.out 21
Hobbyist is correct - the behaviour is UNDEFINED. All arguments of the form "my compiler does X therefore you are wrong" are nonsense. Undefined means the exact implementation is up to the compiler writer. So you could get different results from different compilers (and as we can see from the discussion, that's exactly what's happening). All x++ means is that x is incremented "some time" after the value of x is taken. And ++x means x is incremented "some time" before the value is taken. The precise definition of "some time" is left to the compiler writers. So the code int x=0; int i=x++ + x++; could take the value 0 and increment x to 1, then take the value 1 and increment x to 2, making i=1. Or it could take the value 0 twice, perform the addition, then increment x twice, making i=0. Both are equally valid. The best solution here is NOT to use more than one pre-/post-/inc-/dec-rement operator in an expression, then to modify the variables in separate code. If the intent of "t=--a+--a+a+++a;" was to do something like: "int a=7; a-=2; t=4*a; a++;", then that is what you should do, and this will be both clear and portable, and more importantly, defined (a will be 6 at the end, and t will be 4*5=20).