Please, can anybody explain this #C #programming #riddle? It was originally intended as example for first lesson of C course for beginners, but I fell into trap!
The source:
#include <stdio.h>
void main(void)
{
int a=1, b=1;
printf("Hello++: a==%d, b==%d, a++==%d, ++b==%d, a==%d, b==%d, a--==%d, --b==%d, a==%d, b==%d\n",a,b,a++,++b,a,b,a--,--b,a,b);
}
The output, when compiled by #gcc:
Hello++: a==1, b==1, a++==0, ++b==1, a==1, b==1, a--==1, --b==1, a==1, b==1
(Hint: it works as expected, if you separate the single printf into 5 printf-s with 2 arguments each. The problem is unexpected behavior of C++-ish ++ and -- operators, when used multiple times as multiple function arguments. Remainds me of multiple evaluation of macro arguments, or something like that)







