Vishvendra Singh
Vishvendra Singh

Reputation: 187

C equivalent operator creating confusion

Program 1:

#include<stdio.h>
void main()
{
    int i=55;
    printf("%d %d %d %d\n",i==55,i=40,i==40,i>=10);
}

Program 2:

#include<stdio.h>
void main(){
    int i = 55;
    if(i == 55) {
        printf("hi");
    }
}

First program give output 0 40 0 1 here in the printf i == 55 and output is 0 and in the second program too i ==55 and output is hi. why

Upvotes: 6

Views: 194

Answers (6)

Devolus
Devolus

Reputation: 22074

In the first example, the operators are evaluated in reverse order because they are pushed like this on the stack. The evaluation order is implementation specific and not defined by the C standard. So the squence is like this:

  1. i=55 initial assignment
  2. i>=10 == true 1
  3. i==40 == false (It's 55) 0
  4. i=40 assignment 40
  5. i==55 == false (It's 40) 0

The second example should be obvious.

Upvotes: 9

urzeit
urzeit

Reputation: 2909

The arguments of printf are evaluated from right to left here (this is unspecified behavior, but the behaviour you noticed shows that at least the first argument is evaluated after the second). In your argument list there ist i=40, which sets the value to 40. Therefore the first printed argument is false (0).

Upvotes: 2

rullof
rullof

Reputation: 7424

In the first program: The order of evaluation of the printf() arguments is implimentation defined. So this program doesn't give the same result on different implimentation of printf() So there is no guarantee about what the program will result.

It could output 0 40 0 1 as it could output 1 40 1 1 if the order of evaluation is reversed.

Upvotes: 2

Jerry Coffin
Jerry Coffin

Reputation: 490058

There's no guarantee about the order in which arguments to a function are evaluated. There's also no sequence point between evaluating the different arguments to the function.

That means your first call gives undefined behavior, because it both uses the existing value of i and writes a new value to i without a sequence point between the two.

In a typical case, however, each argument to a function will be evaluated as a separate expression, with no interleaving between them. In other words, the compiler will impose some order to the evaluation, even though the standard doesn't require it to do so.

In the specific case of a variadic function, the arguments are typically pushed onto the stack from right to left, so the first argument (the format string) will be at the top of the stack. This makes it easy for printf to find the format string, and then (based on that) retrieve the rest of the arguments from further down the stack.

If (as is fairly typical) the arguments are evaluated immediately prior to being pushed on the stack, this will lead to the arguments being evaluated from right to left as well.

Functions with a fixed number of arguments aren't evaluated from right to left nearly as often, so if (for example) you wrote a small wrapper taking a fixed number of arguments, that then passed those through to printf, there's a greater chance that i would have the value 55 when the first argument to printf is evaluated, so that would produce a 1 instead of a 0.

Note, however, that neither result is guaranteed, nor is any meaningful result guaranteed--since you have undefined behavior, anything is allowed to happen.

Upvotes: 5

Andrey Mishchenko
Andrey Mishchenko

Reputation: 4206

The reason is that the order in which the arguments to the printf function (actually any function) are evaluated, before being passed to the function, is not defined by the C standard.

The problem is not with your understanding of the == operator.

Edit: Although many authors are pointing out that printf typically evaluates its arguments from right-to-left (which I wasn't aware of), I would say it's probably a bad idea to write code that depends on this, as the language does not guarantee it, and it makes the code much less readable. Nevertheless is is a good factoid to know in case you come across it in the wild.

Upvotes: 2

Abhishek
Abhishek

Reputation: 2673

The function printf evaluates the expressions in implementation specific manner, in this case from right to left. Hence the output:

i==55(0),i=40(Assignment),i==40(0),i>=10(1)

Upvotes: 2

Related Questions