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)

@xChaos The order of evaluation of function arguments is undefined in both C and C++

@mvidner Ok. I suspected this.

It makes some sense: a-- evaluates as 1, returns 0, a++ evaluates as 0 and returns 1. So it possibly just evaluates arguments from right to left.

BTW this is gcc beaviour, I was told, that in clang it is evaluated left to right.

Because printf() is function with variadic arguments, it may be artifact of how va_args() is currently implemented in gcc (?). The right-to-left evaluation may make some sense, because the results of evaluation are sequentially placed on the stack... but I am really not sure.

@mvidner if compiled with

-Wall

it really produces warning: operation on ‘a’ may be undefined [-Wsequence-point]