3

The first code is :

#include <stdio.h>
int main() {
   int a,b,c;
   a = 4;
   b = ++a;
   c = b + a++;
   printf("%d",c);
   return 0;
}

Output is 10

The second code is :

#include <stdio.h>
int main() {
   int a,b;
   a = 4;
   b = ++a + a++;
   printf("%d",b);
   return 0;
}

The output is 11

The two codes should give 10 as the output I believe. Am I doing anything wrong? Can someone explain the above two scenarios?

Harri
  • 39
  • 3
  • 2
    Read this response on [undefined behavior](https://www.geeksforgeeks.org/undefined-behavior-c-cpp/) regarding C increment/decrement operators: https://stackoverflow.com/a/949443/421195. See also C [sequence points](https://en.wikipedia.org/wiki/Sequence_point). Good question: I hope that helps. – paulsm4 Mar 29 '23 at 19:01

0 Answers0