Join Nostr
2026-03-28 10:29:20 UTC

Chao-c' on Nostr: Please, can anybody explain this #C #programming #riddle? It was originally intended ...

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:

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)