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 6 of 7.
Abiyot Hordofa said:
1 decade ago
I am very appreciate you to give me such like chance to see myself how I understand the course!!!!
Thanks to you!
Thanks to you!
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.
Karthikeyan said:
1 decade ago
Step 1: int i=3
Step 2: i=i++ post increment so still i=3 in this statement
Step 3: after completion of the above statement i will be increment by 1 so i becomes 4
step 4: in printf statement i value is printed i.e:4
Step 2: i=i++ post increment so still i=3 in this statement
Step 3: after completion of the above statement i will be increment by 1 so i becomes 4
step 4: in printf statement i value is printed i.e:4
Juber said:
10 years ago
int i = 3;
i = 3+3;
i = 6;
i++;
i = 7;
i++;
i = 8;
Finally i = 8 will be print;
i = 3+3;
i = 6;
i++;
i = 7;
i++;
i = 8;
Finally i = 8 will be print;
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.
Karthik said:
9 years ago
I tried this code in code block and I got the output as 3. But here, the compiler says 4. Why?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers