C Programming - Expressions - Discussion

Discussion Forum : Expressions - Find Output of Program (Q.No. 12)
12.
What will be the output of the program?
#include<stdio.h>
int main()
{
    int i=2;
    printf("%d, %d\n", ++i, ++i);
    return 0;
}
3, 4
4, 3
4, 4
Output may vary from compiler to compiler
Answer: Option
Explanation:

The order of evaluation of arguments passed to a function call is unspecified.

Anyhow, we consider ++i, ++i are Right-to-Left associativity. The output of the program is 4, 3.

In TurboC, the output will be 4, 3.

In GCC, the output will be 4, 4.

Discussion:
43 comments Page 4 of 5.

Bhushan said:   1 decade ago
@Shefali : Difference between GCC and Turbo C compiler is that they are made by different companies.

But the standard for C language is same.

Then you must be asking why their is so much difference in output in two compilers.

So always consider GCC as prime because it is industrial standard C compiler. And Turbo C is old compiler which is not used anymore now except educational purpose to simplify complexity.

Keerthi said:   1 decade ago
Could you please tell me the exact difference as to why 4 4 is printed in gcc compiler where as 4 3 in turbo c.

Shefali sethiya said:   1 decade ago
Where are the difference between GCC compiler and Turbo C compiler ?

P.Vijay kumar said:   1 decade ago
Hi Friends...
The evalution order in c is:Right to left but in c++ is Left to Right.
During the display, the values are dispaly from left to right.(both c and c++)

#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}


O/P : 0 1 30


For eg::
main()
{
int i=5;
printf("%d %d",++i,++i);
}

O/P:: in c::7,6 but in c++ 6,7
But it varies depends on the compiler..

Yami said:   1 decade ago
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}

Please explain it clearly ....

o/p : 0 0 30

Ravikiran said:   1 decade ago
It is right.. for example:


#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}

will u see the out put in this .. the out put will be
0 0 30
because it can read right to left in print statement, when it we write the options continuously(a==40,a<=50,a=30).

Neethu said:   1 decade ago
printf("%d, %d", ++i, ++i)

Here arguments in fxn prnitf() are evaluated from right to left. It is a rule in C. And at time of printing they are displayed in the Left to Right basis.

Pooja said:   1 decade ago
Please explain it clearly with examples.

Kamal said:   1 decade ago
printf("%d, %d", ++i, ++i)
Here arguments in fxn prnitf() are evaluated from right to left. It is a rule in C. And at time of printing they are displayed in the Left to Right basis.

Pruthvi said:   1 decade ago
Is associativity considered in the printf function also or only while evaluating an expression we consider the associativity ?

Can any one explain it. ?


Post your comments here:

Your comments will be displayed after verification.