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;
}
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 3 of 5.
Salim shaikh said:
8 years ago
Main
{
int I=23;
int J
J=I++;
Printf(",,...d",J);
}
Please tell me in this case why error occurs?
{
int I=23;
int J
J=I++;
Printf(",,...d",J);
}
Please tell me in this case why error occurs?
Usha said:
3 years ago
But its prints the latest value 4, 4 only. Am I right?
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.
Rachit said:
1 decade ago
Will the basic rule chan of post/pre increment/decrement in GCC ?
Pratik said:
1 decade ago
Please elabortate it.
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. ?
Can any one explain it. ?
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.
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.
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.
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.
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).
#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).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers