C Programming - Expressions - Discussion
Discussion Forum : Expressions - Find Output of Program (Q.No. 9)
9.
What will be the output of the program?
#include<stdio.h>
int main()
{
int i=3;
i = i++;
printf("%d\n", i);
return 0;
}
Discussion:
70 comments Page 4 of 7.
Sandy said:
8 years ago
@ALL.
int x=3;
i=i++; which means i=i+1; => i=4;
printf("%d",i); prints 4.
int x=3;
i=i++; which means i=i+1; => i=4;
printf("%d",i); prints 4.
Vrushabh said:
8 years ago
Isn't it depend on the compiler. I mean in c++,
int x = 10;
x = ++x;
cout<<x;
it will still give 10 as output.
int x = 10;
x = ++x;
cout<<x;
it will still give 10 as output.
Ankita khatri said:
8 years ago
It's a post increment and the value of i will be added before.
Prof said:
8 years ago
According to me, the answer is 3. Run the code in the compiler.
GuruSudharshan said:
8 years ago
Hi All, Answer is wrong and the correct answer is 3, how means;
int i=3;
i=i++;
in above line i post incremented but result (i++ value is 3 in current line)3 is assigned to i.
So final output is 3.
(if you want output as 4 try this,
int i=3;
i=++i;
)
int i=3;
i=i++;
in above line i post incremented but result (i++ value is 3 in current line)3 is assigned to i.
So final output is 3.
(if you want output as 4 try this,
int i=3;
i=++i;
)
Prashant said:
9 years ago
According to me the answer is 3.
Piyush joshi said:
9 years ago
I tried this code in DEV C++ blog and I got the output as 3. But here, the compiler says 4. Why?
Gaurank Verma said:
9 years ago
Well, careful guys,
First of all let me make this very clear that,
----> priority of "Post Increment" is less than that of "Assignment Operator".
So the first assignment will be done then increment in value of 'i' will take place.
That means, "i" is initialized with 3.
Hence,
3 will be assigned to 'i'. // i ==> i = 3.
But after assignment value of "i" will get increase by 1. // i++ ==> i = 4.
Therefore,
New updated value of "i" will be 4. And we printing value of 'i' in printf () statement.
If the program had been written like this then the output will be '3'.
#include
int main ()
{
int i=3;
int m;
m= i++;
printf ("%d\n", m) ;
Return 0;
}
Here, we are the printing value of m . the output will be '3'.
But,
In the given question. We are the printing value of 'i'.
Hence, Option (B) i.e. 4 is correct.
First of all let me make this very clear that,
----> priority of "Post Increment" is less than that of "Assignment Operator".
So the first assignment will be done then increment in value of 'i' will take place.
That means, "i" is initialized with 3.
Hence,
3 will be assigned to 'i'. // i ==> i = 3.
But after assignment value of "i" will get increase by 1. // i++ ==> i = 4.
Therefore,
New updated value of "i" will be 4. And we printing value of 'i' in printf () statement.
If the program had been written like this then the output will be '3'.
#include
int main ()
{
int i=3;
int m;
m= i++;
printf ("%d\n", m) ;
Return 0;
}
Here, we are the printing value of m . the output will be '3'.
But,
In the given question. We are the printing value of 'i'.
Hence, Option (B) i.e. 4 is correct.
Punit Sharma said:
9 years ago
All of you are wrong. The post-increment effect is damaged by the assignment operator. Thus the value of i=3.
Rahul Agrawal said:
9 years ago
Always go with the last operation here i++ is the last operation so the answer is 4.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers