Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does + sign after variable mean?

Tags:

c++

c

what will be the output of following code
int x,a=3;
x=+ +a+ + +a+ + +5;
printf("%d %d",x,a);

ouput is: 11 3. I want to know how? and what does + sign after a means?

like image 941
user980089 Avatar asked Apr 28 '12 07:04

user980089


1 Answers

I think DrYap has it right.

x = + + a + + + a + + + 5; 

is the same as:

x = + (+ a) + (+ (+ a)) + (+ (+ 5));

The key points here are:

1) c, c++ don't have + as a postfix operator, so we know we have to interpret it as a prefix

2) monadic + binds more tightly (is higher precedence) than dyadic +

Funny isn't it ? If these were - signs it wouldn't look so strange. Monadic +/- is just a leading sign, or to put it another way, "+x" is the same as "0+x".

like image 53
Julian Avatar answered Sep 28 '22 13:09

Julian