1

Today I saw an anonymous behavior of the printf() function. Can anybody please tell me why its behaving so. Is that the execution of functions inside printf() is in reverse order? Please explain this or share a helpful link.

MY CODE

#include <stdio.h>
int fun(){
    static int c=15;c++;
    return c;
}
int main()
{
    printf("%d %d %d",fun(),fun(),fun());
}

Actual output : 18 17 16 Expected output : 16 17 18

EDIT 2: I more thing I noticed that its behavior is not only with functions but also with variables

#include <stdio.h>
static int c=15;
int fun(){
   c++;
   return c;
}
int main()
{
   printf(" %d %d %d %d %d",c,fun(),fun(),fun(),c);
}

Actual output : 18 18 17 16 15 Expected output : 15 16 17 18 18

Thanks in Advance :)

Razib
  • 10,965
  • 11
  • 53
  • 80
Anonymous
  • 297
  • 1
  • 4
  • 13
  • 9
    You are in the land of *undefined behaviour*. Watch for demons. – Greg Hewgill Jun 02 '15 at 04:22
  • 1
    the only thing you can be certain of in your call to printf is that all arguments have been evaluated before control is passed to printf, the order in which they are evaluated is left to the compiler. – AndersK Jun 02 '15 at 04:27
  • 1
    Undefined behavior never ceases to amaze me – asimes Jun 02 '15 at 04:29
  • 1
    Btw, word "anonymous" probably does not mean what you think. Perhaps you were after word "anomalous", which would make some sense. – hyde Jun 02 '15 at 04:40
  • 1
    @hyde or maybe it means that nobody would publicly put their name to this code..:) – M.M Jun 02 '15 at 04:46
  • @GregHewgill there is no undefined behaviour here. A function call introduces a sequence point (or sequenced-before relation) – M.M Jun 02 '15 at 04:47
  • Its undefined behavior.Clang compiler is giving the desired output. – aakansha Jun 02 '15 at 04:49

6 Answers6

2

The order of evaluation of the parameters is unspecified. That means that it is dependent of the compiler implementation. The actual order from your example seems to be:

printf("%d %d %d",fun(),fun(),fun());
/*                (3)   (2)   (1) */

but this is arbitrary and could well be any one out of the 6 possibilities.

Tarc
  • 3,214
  • 3
  • 29
  • 41
2
int main()
{
    int a, b, c;
    a = fun();
    b = fun();
    c = fun();
    printf("%d %d %d", a, b, c);
}

And you get what you need.

The parameter evaluation order is under compiler's control but the statement evaluation order is under your control.

dlask
  • 8,776
  • 1
  • 26
  • 30
  • thanks for your answer but the question was why its behaving so – Anonymous Jun 02 '15 at 04:36
  • Yes, of course. I only wanted to demonstrate that even if the parameter evaluation order cannot be affected, the statement evaluation order is under your control. – dlask Jun 02 '15 at 04:38
1

This behavior seen because of the static variable. You may know A static variable inside a function keeps its value between invocations.

So the variable changed by the earlier call to the function fun() remains for the later call to the function. And the reason for the disordering of the values is -

The order that function parameters are evaluated is an unspecified behavior. It is only ensured that all parameters must be fully evaluated before the function is called.

You may have a look this link for some common undefined behavior in C and C++

Community
  • 1
  • 1
Razib
  • 10,965
  • 11
  • 53
  • 80
0

A similar question was asked here: Parameter evaluation order before a function calling in C

Essentially, the compiler is free to evaluate parameters to a function in any order.

When asked about common undefined behaviours in C, one user answered with this, which also answers your question.

Community
  • 1
  • 1
Jake
  • 154
  • 9
0

The order in which the parameters to a function in not defined in the standard, and is determined by the calling convention used by the compiler.

You can refer this

Community
  • 1
  • 1
prince
  • 1,129
  • 1
  • 15
  • 38
-1

I run the program, and get an output 18 17 16.

That means the different fun()'s run from the right to the left.

However, it is still an undefined behaviour. The order of them is not defined clearly in C, and it depends on the compiler.

Tianyi Hao
  • 59
  • 8