Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 05-02-2003, 10:26 AM   #1 (permalink)
ap
 
Newb Techie

Join Date: May 2003

Posts: 3

ap

Default Cheap C Doubt

Why is there a difference in the evaluation of these two code parts (Atleast in BTC 2.0 and BTCPP 3.0 which i use)


int a=1,b;
b=a++ +a;
cout<<b<<endl; //evaluates to 2
a=1;
cout<<a++ +a; //evaluates to 3

i also suppose the use of
cout<<(a++ +a);
also shows 3. Why does this happen ?
This is also the case in case a printf() is used in C.

The printing of b i can understand, as since it is a post increment, it is evaluated in the end. But in most books, how is it that the post increment is mentioned on the top in the precedence list (some books show it above the pre-increment also). ?

ap is offline  
Old 05-03-2003, 10:48 AM   #2 (permalink)
MM
 
Newb Techie

Join Date: Apr 2003

Posts: 15

MM

Default

Google / Read about 'sequence points' in C++
MM is offline  
Old 02-01-2006, 07:00 AM   #3 (permalink)
simple's Avatar
 
True Techie

Join Date: Jan 2006

Location: Earth

Posts: 171

simple

Default

hey dude! i tried out both your codes( C and C++) in linux( vi editor) and i have found that in both cases i am getting the answer as 2. so check your compiler and try out the same code in a different compiler. c ya!!!!
simple is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On