5

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?

user980089
  • 364
  • 1
  • 3
  • 11

5 Answers5

19

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".

Julian
  • 1,522
  • 11
  • 26
  • yeah, that make sense. There are spaces between + signs but if you remove spaces it shows compilation error:`Lvalue required`. – user980089 Apr 28 '12 at 07:56
  • 3
    Yes, because if you remove the space between `+ +`, you get a single `++` (increment operator) instead of two plus signs. – Mr Lister Apr 28 '12 at 07:58
  • 1
    `+x` is supposed to be like `0+x` for whatever `0` exists for the type of `x`, but there's no such guarantee under overloading. It could be called the "decay operator" because it returns a pure value even if its operand is an lvalue. For example, `++ + a` is illegal except for overloads, and will conventionally not increment `a`. – Potatoswatter Apr 28 '12 at 08:52
  • Good points. The overloading point only applies to c++. The "pure value" point should be true for c and c++. Feel free to edit the answer. – Julian Apr 28 '12 at 09:20
13

The + after a just gets seen as a + before the next value. If you use consistent spacing it is the same as:

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

But not all the +s are necessary so it will act the same as doing:

x = a + a + 5;

The value of a is unchanged because you have never used the incrementing operator which is ++ with no white space between the two + symbols. + and ++ are two separate operators.

DrYap
  • 6,525
  • 2
  • 31
  • 54
  • 4
    I would add that C has a unary `+`, for symmetry with the unary minus `-`, and that this is what is used here. No-one knows what the unary plus is for except confusing programmers. http://msdn.microsoft.com/en-us/library/s50et82s(v=vs.71).aspx – Pascal Cuoq Apr 28 '12 at 07:46
  • but in this case it is not happening as value of a is not incremented. There are 3 + signs before second a. – user980089 Apr 28 '12 at 07:48
  • What do you mean is not happening? – DrYap Apr 28 '12 at 07:50
  • 1
    @user980089: "is not incremented" You know that "+5" means "5", right? So "+a" means "a". "+ + a" is not the same thing as "++a". – SigTerm Apr 28 '12 at 07:55
  • 1
    @PascalCuoq it has at least one use; see my comment to Julian's answer above. – Potatoswatter Apr 28 '12 at 08:53
3

The code seems to be equivalent to:

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

I.e. x = a + a + 5. Which is 11. You know that you can put + or - sign before number, right? Now those + merely indicate sign of variable. Since sign is +, variable remains unchanged I.e. "+5" means "5", so "+a" means "a", and "+ +a" means "+(+a)" which means "a". In same fashion you could write x = + + + 3 + + + + 3 + + + + 5. Or x = - + + - 3 + - + - 3 - - + 5;.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
3

Since the + operators are never two next to each other but always separated by a white space the statement x=+ +a+ + +a+ + +5; is actually read as

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

so basically the final equation becomes of the sort x=a+a+5; and hence the result.

Anurag Ramdasan
  • 4,189
  • 4
  • 31
  • 53
0

x=+ +a+ + +a+ + +5 : This is equivalent to

x = x=+ +a+ + +a+ + +5 or we can write it as x = + (+ a) + (+ (+ a)) + (+ (+ 5)) and the +'s are only indicating the signs which will be finally evaluated as x = a + a + 5.

john
  • 1,323
  • 2
  • 19
  • 31