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 1 of 7.
Divyam Singh Negi said:
2 years ago
I think the correct answer is 3.
(9)
Mohamed elbaradey said:
2 years ago
@All.
Let's go through the code step by step to understand why:
int i = 3;: Initializes the integer variable i with the value 3.
i = i++;: This statement is a bit tricky and involves undefined behaviour in C. It is an example of using the post-increment operator (i++) in an assignment.
Here's what happens in detail:
The value of i is read, which is 3.
The post-increment operator i++ is used, which increments the value of 'i' but returns the old value (3 in this case).
The result of the post-increment operation (3) is assigned back to i. So, 'i' is now set to 3.
Due to the undefined behaviour, the C standard does not specify the order of evaluation for the two side effects (increment and assignment) on the same variable within the same sequence point.
Therefore, the output of printf("%d\n", i); will be 3.
Let's go through the code step by step to understand why:
int i = 3;: Initializes the integer variable i with the value 3.
i = i++;: This statement is a bit tricky and involves undefined behaviour in C. It is an example of using the post-increment operator (i++) in an assignment.
Here's what happens in detail:
The value of i is read, which is 3.
The post-increment operator i++ is used, which increments the value of 'i' but returns the old value (3 in this case).
The result of the post-increment operation (3) is assigned back to i. So, 'i' is now set to 3.
Due to the undefined behaviour, the C standard does not specify the order of evaluation for the two side effects (increment and assignment) on the same variable within the same sequence point.
Therefore, the output of printf("%d\n", i); will be 3.
(5)
Sakthi said:
2 years ago
It's a post-increment. So the right answer is 3.
(3)
Dhananjay Khairnar said:
4 years ago
3 is the Right Answer.
(3)
Riddhi Mitkari said:
2 years ago
Correct answer is 4.
because i=3
i=i++ // this is post increment so i=3 but internally i is increment by 1.
so i=4.
because i=3
i=i++ // this is post increment so i=3 but internally i is increment by 1.
so i=4.
(2)
Isha said:
3 years ago
As per my knowledge the coding part is;
#include<stdio.h>
int main()
{
int a=(1,2,3);
b=(++a,++a,++a);
c=(b++,b++,c++);
printf("%d %d %d \n", a,b,c);
return 0;
}
#include<stdio.h>
int main()
{
int a=(1,2,3);
b=(++a,++a,++a);
c=(b++,b++,c++);
printf("%d %d %d \n", a,b,c);
return 0;
}
(2)
Divya Reddy said:
4 years ago
Yes, agree the answer is 3.
(2)
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)
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)
Priyanka said:
5 years ago
The answer is 3 because post-increment.
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers