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 2 of 5.

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

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.

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.

Radha said:   1 decade ago
The o/p of above explained program is not 0030
Its 0130
Execution in that s 4m right to left
STEP1: FIRST the variable IS assigned as 35 at the time of declaration
STEP2: Then in the printf statement variable a is assigned as a=30(as execution is from right to left)
step 3: Then a<=50 becomes 30<=50 &the value of that relational expression is 1(30<=50 is true)
step 4: Then a==40 becomes 30==40&its value is zero
The execution is from right to left but the values are printed as per the seqence. So ans is 0130.

RAHUL said:   1 decade ago
OUTPUT ? AND WHY THAT O/P?

#include<stdio.h>
int main()
{
int i=2;
int j=3;
printf("%d, %d\n", ++j, ++i);
return 0;
}

Rohit nagappa said:   1 decade ago
Output will be 3 and 4 as in printf() function expression is evaluated from right to left. and displayed from left to right. As here ++i will be evaluated first then i becomes 3 and than ++j will be evaluated than j becomes 4.

But as output is left to right.

Hence output is 4 3 (gcc compiler).

Sharath said:   1 decade ago
Could you please tell me the exact difference as to why 4 4 is printed in c compiler whereas 4 3 in gcc?

.

Sonam said:   1 decade ago
What is the difference between ++i and i++. Can anybody explain with an example?

Neethu said:   1 decade ago
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d %d %d ",a++,a++,++a,a++,++a);
return 0;
}
OUTPUT gcc: 39 38 40 36 40.

Suresh said:   1 decade ago
Hey @Neethu can you please elaborate this prog at printf function.


Post your comments here:

Your comments will be displayed after verification.