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 4 of 7.
Nagamani said:
6 years ago
No, the actual answer is 3 because it is the dependent expression the value of I will assigned to the left side and than value incremented.
Gunjan said:
5 years ago
3 is the correct answer.
As x=++i i.e. y=++i
At the place of x, any other variable can be there.
As x=++i i.e. y=++i
At the place of x, any other variable can be there.
PRASHANT MARATHE said:
5 years ago
Post increment -- after assigning the value to the variable the value is incremented. i.e 3.
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++.
Aarti Sonawane said:
1 month ago
Good explanation, thanks all.
Shashi said:
1 decade ago
@All above are wrong : Works from right to left.
The statement i = i++ means i == i + 1;.
Step 1: i = i + 1 results i = 4.
Step 2: i = 4.
The statement i = i++ means i == i + 1;.
Step 1: i = i + 1 results i = 4.
Step 2: i = 4.
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.
OmPrakash said:
1 decade ago
Has anyone compile the code? Do it and you will find '3' is the answer.
Dixit said:
1 decade ago
First i = 3 will be assigned to i (i = i++).
Then i will be incremented to 4.
Then i will be incremented to 4.
Govindaraju said:
1 decade ago
i=i++; => i =3;
After executing this statement the i value is i=i.
+1 => 4.
As it is post decrement operator the value will be changed after the execution of the statement.
After executing this statement the i value is i=i.
+1 => 4.
As it is post decrement operator the value will be changed after the execution of the statement.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers