DIGIT-ARC read-only archive · snapshot: July 23, 2026

C Expression Problem

13 posts · originally from https://geek.digit.in/community/threads/c-expression-problem.159228/

tech_boy
tech_boy Original post #1
Jun 27, 2012 at 11:56 PM
Guys consider following statements:
int a,b,c;
a=5;
b=7;
c=a+++++b;

What is stored in c?and How!
krishnandu.sarkar
Jun 28, 2012 at 12:36 AM
Re: C Programming Problem

Well, If I'm not wrong then the answer would be

c = (a++) + (++b)
c = 5 + 8
c = 13

And after the operation a = 6, b = 8, c = 13.

a++ = Post Increment, so the value is increased after the operation performed.
++b = Pre Increment, so the value is incremented before the operation is performed.
tech_boy
Jun 28, 2012 at 9:34 AM
Re: C Programming Problem

@neuron: yes it is giving compiler error if we don't use parenthesis. But why?
nims11
nims11 #5
Jun 28, 2012 at 10:36 AM
Re: C Programming Problem

@neuron: yes it is giving compiler error if we don't use parenthesis. But why?

because of precedence. the precedence is as follows
expr++ > + > ++expr.

So
a+++++b is equivalent to (a++)+++b
In x+++b '+' operation will be performed first and you can see why it won't be possible here and thus compiler throws an error.
RBX
RBX #6
Jun 28, 2012 at 10:59 AM
Re: C Programming Problem

It works fine if I rewrite it as c=a++ + ++b;

I would call it compiler's limitation, the white spaces should be meaningless.
tech_boy
Jun 28, 2012 at 11:15 AM
Re: C Programming Problem

Tested using Dev c++, but the error continues.
n64freak
Jun 28, 2012 at 11:50 AM
Re: C Programming Problem

Why are you trying this out :/

It's a compiler dependant thing, I've seen it run and give answers as well as show an error.
rijinpk1
Jun 28, 2012 at 7:11 PM
Re: C Programming Problem

The first preceedence is given to post increment(a**).then pre increment(**a). Here Addition is given least priority.a** will be correctly compiled. Then if u do not put paranthesis or space compiler would have confusion to go for preincrement or addition, Since compilation takes from left to right. That is why it is getting wrong for u. I dont think it is compiler's fault. Consider c=a******b. How compiler would consider this? c=(a**)*(**(*b)). Will compiler consider so?
It is ur duty to put paranthesis where ever necessary.
I think I am not wrong.
Post ur suggestions

My plus sign is not printed in my above comment.
So pls consider * instead of plus sign. Sorry for the inconvenience.
Neuron
Neuron #10
Jun 28, 2012 at 7:22 PM
Re: C Programming Problem

Tested using Dev c++, but the error continues.

a+++++b;

This line is parsed by the compiler the following way.

1.Operand 'a' is added to the stack.
2.The next operand encountered is '++' .It checks to if there is an lvalue to operate upon.In this case the lvalue is 'a',so no problem and it is added to the stack.
3.The next operand encountered is also '++'. The compiler analyses a statement sequentially, not entirely and so there is no question of analysing the rest of the statement to interpret the operators as '+' and then '++'.Since the operator is a ++ it checks to see if there is an lvalue to operate upon. There's none and hence the error.
Remember 'a+++b' is processed as a++ + b.

If you write a++ + ++b, there is no invalid increment operator.
rijinpk1
rijinpk1 #11
Jun 28, 2012 at 7:33 PM
Re: C Programming Problem

^^ that is what I mentioned in my above comment
"SINCE COMPILATION TAKES FROM LEFT TO RIGHT".
Neuron
Neuron #12
Jun 28, 2012 at 7:45 PM
Re: C Programming Problem

This error has nothing to do with the precedence of operators. It is because the way a statement is processed to extract the operators and operands.
tech_boy
tech_boy #13
Jun 29, 2012 at 8:32 AM
Re: C Programming Problem

@neuron: Yes, u r right, compiler is also giving a 'No lvalue' error. Thanks :)