Hello,
I am new to this forum.
I have a doubt in the post and pre increment operators.
I get c as 7 and d as 4 .. Please explain me how..Code:#include<stdio.h> int main() { int d=1,c; c= d++ + ++d + ++d; printf("%d %d",c,d); return 0; }
Hello,
I am new to this forum.
I have a doubt in the post and pre increment operators.
I get c as 7 and d as 4 .. Please explain me how..Code:#include<stdio.h> int main() { int d=1,c; c= d++ + ++d + ++d; printf("%d %d",c,d); return 0; }
I am getting c equal to 9 and d equal to 4 .
That is because the values of variables are modified after their increments:
Code:#include <stdio.h> int main(void) { int d = 1, c; c = d++ + ++d + ++d; //The line above would generate: c = 2 + 3 + 4; //Now "c" is equal to 9 (2 + 3 + 4) and "d" is equal to 4. printf("%d %d", c, d); getchar(); return 0; }
I hope that I have helped .
This code exhibits undefined behavior. The expression itself is ambiguous because each term can be evaluated in any order, and the compiler is free to do so. Different compilers (or different compiler versions or even different optimization settings in the same compiler) can give you different results, and they would still be correct.
See Expressions for more information about expressions.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.