Just a quick simple question

I have this piece of code copied from my hardcopy book. It's a problem, where you have to find out the output.

#include<stdio.h>
int main()
{
int a,b;
a= -3--3;
b= -3--(-3);
printf("\n a = %d , b = %d",a,b);
return 0;
}

In answer key, it shows the output as 0 and -6. But when i actually ran the program,it gave me these errors:

prog.c:5: error: lvalue required as decrement operand
prog.c:5: error: expected ‘;’ before numeric constant
prog.c:6: error: lvalue required as decrement operand

I can feel too that 5th and 6th lines are errors, But don't know how this error is triggered.

So just tell me why this error is triggered ?

Regards
Legend_Xeon
Last edited on
you are missing spaces on Lines 5 and 6

look at your book carefully and then retype

also, I advise that you use code tags next time
Oh yes. You are right. I just overlooked the code and indeed mistyped. Sorry for that.
it was actually,

1
2
3
a=-3 - -3;
b=-3 - -(-3);


I have two more questions in my mind,

1. Does that statement, a=i++;
expand to these two statements
1
2
a=i;
i=i+1;  //So that first value of i is assigned to a, and then i is incremented 

Is this right to write this expanded code for understanding complex problems ?

2. Does increment/decrement operator work on constants too as in 56++ , --9.56.
While doing that, compiler is generating error: LValue required.
Is this because compilers are made to generate such hard coded error or any syntax has gone wrong ?


Thanks for helping.

Regards

Legend_Xeon
Last edited on
1) Correct. As for whether or not it is moral...it would depend on the situation really. Generally, I would say it is not, but I'm sure there are some situations where it would be acceptable.

2) No, because constants are considered RValues, and those operators (since they modify the argument) need LValues.
Thanks. I understand the situation.
I appreciate your help firedraco & kfmfe04 :-)
Topic archived. No new replies allowed.