printf arguments problem


//why is second printf string being printed first??

The order that function parameters are evaluated is unspecified behavior. (This won’t make your program crash, explode unlike undefined behavior.)

The only requirement is that all parameters must be fully evaluated before the function is called.


// The simple obvious one.

Can be equivalent to this:

int a = getA();
int b = getB();

Or this:

int b = getB();
int a = getA();

It can be either, it’s up to the compiler. The result can matter, depending on the side effects.

Answer credit : Loki Astari StackOverflow link.

Nothing goes “from right to left” or “from left to right” in function argument evaluation. When function arguments are evaluated, the order of evaluation is unspecified and there are no sequence points between evaluating separate arguments. This means that there’s absolutely no temporal ordering in this process. The arguments can be evaluated in any order, and the process of their evaluation can be intertwined in any way.

Answer credit : AnT StackOverflow link.

Now I guess you can understand why the second string is being printed first ( this may vary depending upon compiler version, architecture etc. )

In C, printf() returns the actual character count. At first the inner function calls to printf are executed in some order and this opuputs " buewbdnajs " or " dnajsbuewb ". Then the character count is returned to the calling ( outermost ) printf and it prints " 55 " ( because this is the charecter count for both " buewb " and " dnajs ").

So, outputs can either be buewbdnajs55 or dnajsbuewb55

If interested, you can also go through this wiki link.