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.
                
                        Aarti Sonawane said: 
                         
                        4 months ago
                
                Good explanation, thanks all.
                
                        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.
                     (6)
                
            
                        Divyam Singh Negi said: 
                         
                        2 years ago
                
                I think the correct answer is 3.
                
                     (10)
                
            
                        Sakthi said: 
                         
                        3 years ago
                
                It's a post-increment. So the right answer is 3.
                
                     (4)
                
            
                        Riddhi Mitkari said: 
                         
                        3 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)
                
            
                        Dhananjay Khairnar said: 
                         
                        4 years ago
                
                3 is the Right Answer.
                
                     (3)
                
            
                        Divya Reddy said: 
                         
                        4 years ago
                
                Yes, agree the answer is 3.
                
                     (2)
                
            
                        Coolant said: 
                         
                        4 years ago
                
                Answer will be 3.
i=i++
i=3++
I take the value 3 there won't be an increment.
Priority will be given to i=3 not to i++.
                i=i++
i=3++
I take the value 3 there won't be an increment.
Priority will be given to i=3 not to i++.
                        PRASHANT MARATHE said: 
                         
                        5 years ago
                
                Post increment -- after assigning the value to the variable the value is incremented. i.e 3.
                Post your comments here:
 
            
        Quick links
                            Quantitative Aptitude
                                    
                                    Verbal (English)
                                    
                                    Reasoning
                                    
                                Programming
                                    
                                    Interview
                                    
                                     Placement Papers