C Expression Problem

tech_boy

The Annoying Thing
Guys consider following statements:
int a,b,c;
a=5;
b=7;
c=a+++++b;

What is stored in c?and How!
 

krishnandu.sarkar

Simply a DIGITian
Staff member
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.
 
OP
tech_boy

tech_boy

The Annoying Thing
Re: C Programming Problem

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

nims11

BIOS Terminator
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

In the zone
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.
 

n64freak

Broken In
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

Aspiring Novelist
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

Electronic.
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.
 
Last edited:

rijinpk1

Aspiring Novelist
Re: C Programming Problem

^^ that is what I mentioned in my above comment
"SINCE COMPILATION TAKES FROM LEFT TO RIGHT".
 

Neuron

Electronic.
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.
 
OP
tech_boy

tech_boy

The Annoying Thing
Re: C Programming Problem

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