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 1 of 5.
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)
Piyush Watmode said:
4 years ago
Actully, the output in gcc compiler is 4, 4.
(1)
Ganesh said:
7 years ago
I am not getting it. Please explain me.
(1)
Pavani said:
10 years ago
Sorry displayed in right to left order.
Deepanshu said:
1 decade ago
@Neethu:
The output in turbo C compiler is :
39 38 38 36 36.
The output in turbo C compiler is :
39 38 38 36 36.
Ganesh marmat said:
1 decade ago
Hi friends.
In gcc compiler the statement use stack code of:
#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.
And only add to it a++ statement.
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d %d %d %d",a++,a++,++a,a++,++a,a++);
return 0;
}
What is output?
Stack contain 36,37,38,39,40,41 when first %d print 40 because a++ post increment if ++a o/p is 41.
2nd %d o/p is 39 if a++ o/p 41.
All post increment represent it stack value and pre increment is higest value.
In gcc compiler the statement use stack code of:
#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.
And only add to it a++ statement.
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d %d %d %d",a++,a++,++a,a++,++a,a++);
return 0;
}
What is output?
Stack contain 36,37,38,39,40,41 when first %d print 40 because a++ post increment if ++a o/p is 41.
2nd %d o/p is 39 if a++ o/p 41.
All post increment represent it stack value and pre increment is higest value.
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.
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);
}
#include<stdio.h>
void main ()
{
int a = 3;
int b = ++a + a++ + --a;
printf ("Value of b is %d", b);
}
Suri said:
1 decade ago
Can anyone please explain in a correct way, bit confusing?
Rahul said:
1 decade ago
Why GCC compiler give the different output?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers