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 4 of 5.
Shani dubey said:
10 years ago
i++ is postfix increment and ++i is prefix increment. As example:
void main()
{
int i=1;
i=i++;
printf("%d",i);
}
After execute it print output = 2.
In prefix increment:
main()
{
i=1;
i=++i;
}
After execute it print the output = 2.
void main()
{
int i=1;
i=i++;
printf("%d",i);
}
After execute it print output = 2.
In prefix increment:
main()
{
i=1;
i=++i;
}
After execute it print the output = 2.
Manu said:
10 years ago
Please explain this clearly.
Pavani,RKValley said:
10 years ago
Post and pre increments evaluated in left to right order.
And displayed left to right order;
Example: i = 2;.
++i, --i.
First we have to calculate --i after that ++i.
Those values are 1, 2.
Displayed values are 2, 1.
And displayed left to right order;
Example: i = 2;.
++i, --i.
First we have to calculate --i after that ++i.
Those values are 1, 2.
Displayed values are 2, 1.
Pavani said:
10 years ago
Sorry displayed in right to left order.
Ankit kumar said:
9 years ago
It is mentioned here that in GCC compiler output will be 4 4. How? Explain please.
Kartheek said:
9 years ago
In Linux 4,4 because the priority is from right to left. If pre-increment is there from right to left then the variable is updated.
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.
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.
Sathya said:
8 years ago
int j=5,s=5,i=0,m=5,n=5,q=0,h=5;
i=m++ + ++n-j-- - --s;
q=h++ + ++h - h-- - --h;
System.out.println("q:"+q);
System.out.println("i:"+i);
System.out.println("j:"+j);
System.out.println("s:"+s);
System.out.println("j:"+m);
System.out.println("s:"+n);
can you explain this one?
i=m++ + ++n-j-- - --s;
q=h++ + ++h - h-- - --h;
System.out.println("q:"+q);
System.out.println("i:"+i);
System.out.println("j:"+j);
System.out.println("s:"+s);
System.out.println("j:"+m);
System.out.println("s:"+n);
can you explain this one?
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?
Giriprasad said:
7 years ago
It is prinetd in left to right. If it is printed in right to left then the output is 3, 4.
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers