Question:
c language question in this program { int a=2; a=a++; printf("%d",a); } why the output is 2?
2012-01-18 22:03:11 UTC
c language question in this program { int a=2; a=a++; printf("%d",a); } why the output is 2?
Thirteen answers:
?
2012-01-18 22:21:47 UTC
This is because a++ is post incremented that is first the value of a is assigned to a and the incremented value is being lost.incidentally, writing a=a++ is very poor coding style, for obvious reasons, and is to be avoided in all situations. This might have something to do with memory management in C.I am trying to explain it using a stack.Stack is used in recursion to store the values and it must be something like that here too.But i am not getting any reliable sources.From recursion i understand this principle is right.



A compiled C program creates and uses four logically distinct regions of memory. The first region is

the memory that actually holds the program's executable code. The next region is memory where

global variables are stored. The remaining two regions are the stack and the heap.



"The stack is used for a great many things while your program executes. It holds the return addresses of function calls,arguments to functions, and local variables. It will also save the current state of the CPU. The heap is a region of free memory that your program can use via C's dynamic memory allocation functions"



The important thing to think about in this case is how such expressions are evaluated and how they are executed. The rules are:



1. If the ++ appears BEFORE the variable, the variable's value is incremented FIRST. THEN, the variable's value is resolved.



2. If the ++ appears AFTER the variable, the variable's value is resolved FIRST. THEN, the variable's value is incremented.

OK. Those are the rules. Now an example. I think I'll use a fuller example to explain how this works so you can get into the rhythm of how such expressions are evaluated and executed.



1: int a = 2;

2: a= a++;

3:printf("%d",a); // 2







On line 2, a++ examined. Using rule (2) from above, the variable's value is first resolved, in this case, to 2.This value is pushed into the stack.



Then variable 'a' is then incremented from 2 to 3 by a++. Thus 3 is pushed into stack.

This effectively leaves the program stack as::-



[3]

[2]



Now stack follows the principle of LIFO(Last in first out).So variable a is now assigned values from stack by pop operation.First it is assigned 3 then 2.



...and this is the important point. Although the variable 'i' currently contains 3, that value will be overwritten when the above line of code executes, overwriting the 3, with a 2.



a=a++; //here a is assigned value 2 and that is what is retained





#include

#include



int main(void)

{

int b,a=2;

b=a++; //b is assigned 2 then a is incremented to 3 ::here we have to variables so popped values goes to different addresses

printf("%d",b);

printf("%d",a);

return 0;

}
Cubbi
2012-01-19 11:21:52 UTC
Wow, 10 wrong answers, only roger is correct.



To reiterate, the line "a = a++;" is an ERROR in C, similar to accessing an array out of bounds or dereferencing a null pointer. Anything at all can happen if this erroneous statement is given to a C compiler.



Each compiler that lets this compile deals with this in its own way, and there is no way to predict what it will do. In practice, compilers will either print 2 or 3, but different versions of the same compiler (see gcc below) may print different things. Here's a few tries:



OS: Linux

GNU gcc 4.6.2 prints 3

Clang 3.0 prints 2

Intel 11.1 prints 3



OS: Solaris

GNU gcc 3.4.6 prints 2

SunPro C 5.8 prints 3

SunPro C++ 5.8 prints 2



OS: Windows

Visual Studio 2010 C++ prints 3
Arvind
2012-01-18 22:10:46 UTC
a++ is a postfix operator. it doesnt increment the value at that moment. but at the next instance. this is complex to understand with your question. but the same with this example its easy.



int a=2, b=0;

b=a++;

print b,a;



then the output is, a=3, b=2.



in this program, the value of "a" is first assigned to "b" and then gets incremented
balakrishnan
2012-01-19 02:58:54 UTC
This is a tricky question actually ,

When you run this in TC it will show you 3

When you run in unix environment it will show you 2

And also in java it would show you 2 .



TC - > Wrong implementation of logic.

other two - > first assigment takes place and assigns the un-incremented value to the variable in the left side and then increments the value of the variable in the right hand side.
bapi
2012-01-19 03:26:20 UTC
At first your answer is wrong.It will print 3



++ is the post increment operator.So when the compiler scan the line

a=a++

then it assign the value of a within a then it incrementing the value of a by one and now it's 3.

When you are printing the value of a within printf() it's printing the current value of a.



More fun are waiting for you,Check my site

knowhow.0fees.net
2012-01-18 22:10:28 UTC
a=a++;



This assigns the value of a to the variable a, then increments a by one. It would be functionally correct to write:



a=++a;



which would increment a by one before it is reassigned to variable a. Either way, you can simply replace that whole line with



a++;



because "++", the increment function, is logically equivalent to



a=a+1;
?
2012-01-19 08:55:07 UTC
{

int a=2;

a=a++;

printf("%d",a);

}



Your programme has undefined behaviour.

the statement

a=a++;

changes a twice between sequence points

that is a no no.

http://msdn.microsoft.com/en-us/library/azk8zbxd.aspx



anything can happen!

the compiler can do ANYTHING it wants to with your code.
June
2012-01-18 22:13:01 UTC
a++ is post increment. It reassigns value 2 before incrementing it. So, its still 2.
killer
2012-01-18 23:07:22 UTC
it will always print 3 ..

if u r print like this printf("%d",a++);

than it will print 2. due to post increment .
2012-01-18 22:15:04 UTC
Coz it jst simply assign only 2
Srikanth
2012-01-19 02:55:19 UTC
it prints 3
Karan Patel
2014-09-30 06:40:25 UTC
ans is 2 why i dont know how to perform this operation
Siri
2012-01-18 22:06:50 UTC
i dot know c languge..srry dear


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...