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 3 of 7.
Gudimalla said:
7 years ago
According to me, it is 3.
Patit Pawan Barik said:
7 years ago
@All.
The output will be 3.
#include<stdio.h>
int main()
{
int i=3;
i = i+1;
printf("%d\n", i);
return 0;
}
returns 4 because it is assigned at that moment only whereas
#include<stdio.h>
int main()
{
int i=3;
i = i++;
printf("%d\n", i);
return 0;
}
will return 3 because the value assigned is 3 with post increment.
The output will be 3.
#include<stdio.h>
int main()
{
int i=3;
i = i+1;
printf("%d\n", i);
return 0;
}
returns 4 because it is assigned at that moment only whereas
#include<stdio.h>
int main()
{
int i=3;
i = i++;
printf("%d\n", i);
return 0;
}
will return 3 because the value assigned is 3 with post increment.
Saurav chaurasia said:
7 years ago
#include<stdio.h>
int main()
{
int i=3;
i = i++;
printf("%d\n", i);
return 0;
}
The output will be only 3 not 4 bcoz of post-increment as i=i++(it means that i will not be increased by 1 while 1 is incremented in memory not in the value of i but if in place of (i=i++) only i++ is given then means that value of i is increment by one )
If code is written like this then output will 4.
#include<stdio.h>
int main()
{
int i=3;
i++;
printf("%d\n", i);
return 0;
}
Thank you.
int main()
{
int i=3;
i = i++;
printf("%d\n", i);
return 0;
}
The output will be only 3 not 4 bcoz of post-increment as i=i++(it means that i will not be increased by 1 while 1 is incremented in memory not in the value of i but if in place of (i=i++) only i++ is given then means that value of i is increment by one )
If code is written like this then output will 4.
#include<stdio.h>
int main()
{
int i=3;
i++;
printf("%d\n", i);
return 0;
}
Thank you.
(1)
Prashant said:
7 years ago
It is wrong because of i=3;
i=i++;
first i=4;
seound i++=3;
than
i=i++;
So, i++ override the value of i;
The output will be 3;.
i=i++;
first i=4;
seound i++=3;
than
i=i++;
So, i++ override the value of i;
The output will be 3;.
Somes Kumar K said:
7 years ago
The ans is 3 not 4,
Here, i=i++ is divided into temp=i; i=i++; i=temp so the i value remains unchanged.
Here, i=i++ is divided into temp=i; i=i++; i=temp so the i value remains unchanged.
(1)
Rohan said:
8 years ago
I tried in the gcc compiler in linux and the output is 3. But here it says 4. How, please explain.
Akash said:
8 years ago
The answer should be 3.
Because the postfix works totally different than prefix.
When postfix operator is used it uses another temporary variable while prefix uses the same variable for increment.
NOTE: the temp most probably will be a register.
So here it is how it goes:
i++ -> temp = 4;
But i = i++ ; which means i++ will evolve after solving the assignment and the assignment goes to temp var.
Hope I was clear.
Because the postfix works totally different than prefix.
When postfix operator is used it uses another temporary variable while prefix uses the same variable for increment.
NOTE: the temp most probably will be a register.
So here it is how it goes:
i++ -> temp = 4;
But i = i++ ; which means i++ will evolve after solving the assignment and the assignment goes to temp var.
Hope I was clear.
Tayo said:
8 years ago
I just ran it on my computer and it is 3 not 4. It's a post-increment.
Gaurav said:
8 years ago
@Rishabh.
You're damn right! the answer is 3 in case of such post increment.
i = i++;
is analogous to
temp = i;
i = i + 1;
i = temp;
You're damn right! the answer is 3 in case of such post increment.
i = i++;
is analogous to
temp = i;
i = i + 1;
i = temp;
Rishabh said:
8 years ago
I think the answer is 3. I run it on both GCC complier or turbo C compiler try it on your own.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers