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.

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.

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.

Ranjith karthick said:   10 years ago
How the output for the following produce?

#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.

Sai charan adurthi said:   1 decade ago
What is the output and how did we get it? Can anyone help me please?

#include<stdio.h>

void main ()
{

int a = 3;
int b = ++a + a++ + --a;
printf ("Value of b is %d", b);

}

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

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. ?

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;
}

Ramkumar said:   8 years ago
in turbo C o/p is 4,3.
in c++ o/p is 3,4.
in GCC o/p is 4,4.

Thus the answer will be option D : output will vary from compiler to compiler.

Anu said:   1 decade ago
printf reads from right to left and if it is assigned value before and we again assigned value and we assigned in printf which value does it take.

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.


Post your comments here:

Your comments will be displayed after verification.