You are here:

C/weird syntax a+++b

Advertisement


Question
why if I write the following code:

int a=3, b=5, i;
i=a+++b;
printf  ("%d",i); // 8 is printed

but if I write:
int a=3, b=5;
a=a+++b;
printf ("%d",a_; // 9 is printed

?
thanks,
nir.

Answer
It depends on the way computer interprets the code.
It can interpret it as (a++ + b) or (a + ++b).
So, in the first case it will result in 8 and in second case it will be 9.
Look at the assembly listing and then try to analyze.

The same code may give different result on another compiler!

-Narendra

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.