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.
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
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
P.Vijay kumar said:
1 decade ago
Hi Friends...
The evalution order in c is:Right to left but in c++ is Left to Right.
During the display, the values are dispaly from left to right.(both c and c++)
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}
O/P : 0 1 30
For eg::
main()
{
int i=5;
printf("%d %d",++i,++i);
}
O/P:: in c::7,6 but in c++ 6,7
But it varies depends on the compiler..
The evalution order in c is:Right to left but in c++ is Left to Right.
During the display, the values are dispaly from left to right.(both c and c++)
#include<stdio.h>
int main()
{
int a=35;
printf("%d %d %d ",a==40,a<=50,a=30);
return 0;
}
O/P : 0 1 30
For eg::
main()
{
int i=5;
printf("%d %d",++i,++i);
}
O/P:: in c::7,6 but in c++ 6,7
But it varies depends on the compiler..
Shefali sethiya said:
1 decade ago
Where are the difference between GCC compiler and Turbo C compiler ?
Vishwa said:
1 decade ago
Can anybody explain this...?
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.
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.
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;
}
#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).
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?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers