C programming increment operator related | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C programming increment operator related

int a=3; int b=++a + a++; printf("%d",b); gives output 9 whereas int a; scanf("%d",&a); int b=++a + a++; printf("%d",b); gives output 8 for a=3 Why??

22nd Sep 2018, 9:27 AM
MR. HACKER
MR. HACKER - avatar
1 Answer
+ 6
The produced answers are undefined behaviors ¹. The code is violating the sequencing rule ² by using the increment operator ³ , carelessly. ____ ¹ https://en.wikipedia.org/wiki/Undefined_behavior ² https://en.wikipedia.org/wiki/Sequence_point#Sequence_points_in_C_and_C++ ³ https://en.cppreference.com/w/c/language/operator_incdec
22nd Sep 2018, 11:06 AM
Babak
Babak - avatar