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.
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.
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)
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)
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.
Avinash said:
1 decade ago
Answer = 4 is appropriate among the given options.
To say accurately the answer should be "undefined".
i=i++;// the behavoiur of this expresiion is undefined.
In an expression never use value of a variable if the value of the variable is getting changed..
Similarly,
i=i++*i++;//undefined b/c trying to modify twice with in an expression.
i=i*i++;//undefined.
To say accurately the answer should be "undefined".
i=i++;// the behavoiur of this expresiion is undefined.
In an expression never use value of a variable if the value of the variable is getting changed..
Similarly,
i=i++*i++;//undefined b/c trying to modify twice with in an expression.
i=i*i++;//undefined.
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.
Saurabh said:
1 decade ago
For that question some compiler will give different answer like Dev C++.
Is giving 3 instead of 4 because when a single expression causes the same object to be modified then inspected the behavior is undefined. In this case I = i++;
Here variable I is being modified and then it is being used by the compiler. That's why answer 4 is coming here.
Is giving 3 instead of 4 because when a single expression causes the same object to be modified then inspected the behavior is undefined. In this case I = i++;
Here variable I is being modified and then it is being used by the compiler. That's why answer 4 is coming here.
Sundar said:
1 decade ago
@Karthikeyan You are absolutely correct.
Let me give my points too.
i = i++; // You may have little confusion here.
The above statement will be executed as given below:
Step-1: i = i; // Here i = 3
Step-2: i = i+1; // Here i = 3 + 1 = 4.
Therefore, 4 is the correct answer.
Let me give my points too.
i = i++; // You may have little confusion here.
The above statement will be executed as given below:
Step-1: i = i; // Here i = 3
Step-2: i = i+1; // Here i = 3 + 1 = 4.
Therefore, 4 is the correct answer.
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;
)
Subhajyoti said:
1 decade ago
i is initialised a value 3,which is again assigned to i as the post decrement operator will be executed after completion of the statement.after completion of the statement i value increases to 4 which will be the output.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers