Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the operation c=a+++b mean?

Tags:

c++

c

visual-c++

The following code has me confused

int a=2,b=5,c; c=a+++b; printf("%d,%d,%d",a,b,c); 

I expected the output to be 3,5,8, mainly because a++ means 2 +1 which equals 3, and 3 + 5 equals 8, so I expected 3,5,8. It turns out that the result is 3,5,7. Can someone explain why this is the case?

like image 588
user595985 Avatar asked Sep 20 '11 12:09

user595985


People also ask

What is the meaning of AB in C?

In the C Programming Language, the abs function returns the absolute value of an integer.

What is C operation?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What does a << b mean?

a<<b for integers means "shift left". The bitwise representation of a is shifted left b bits. This is the same as multiplying by (2 to the power of b ).


2 Answers

It's parsed as c = a++ + b, and a++ means post-increment, i.e. increment after taking the value of a to compute a + b == 2 + 5.

Please, never write code like this.

like image 95
Fred Foo Avatar answered Oct 03 '22 05:10

Fred Foo


Maximal Munch Rule applies to such expression, according to which, the expression is parsed as:

c = a++ + b; 

That is, a is post-incremented (a++) and so the current value of a (before post-increment) is taken for + operation with b.

like image 24
Nawaz Avatar answered Oct 03 '22 05:10

Nawaz