Can you have a incrementor and a decrementor on the same variable in the same statement in c
Asked Answered
T

2

1

Is

--foo++;

a valid statement in C? (Will it compile/run) And is there any practical application for this?

Sorry for changing the question in an edit but I found something out.

According to my C++ compiler (Visual Studio 2010):

--++foo;

is a valid command but

foo--++; 

is not. Is there any reason for this?

Theophilus answered 26/7, 2012 at 23:0 Comment(11)
Why? What purpose would this have? (didn't downvote, but this leaves me wondering about why anyone would even want to know this - who would code anything like this)Rebus
The primary question is could this be a legal command.Theophilus
Part one of this question is trial and error. Part two is using your head...Aristotelianism
@nos Not wondering about what would happen, but wondering why this would even come up - I don't see much benefit to code like that.Rebus
@nos The question was if it's a valid statement (i.e. no complaints from compiler). Not what the results are. Determining whether or not --foo++ is a "valid statement" is easily achieved by feeding it to the compiler. No error says "valid".Aristotelianism
Right, I know different compilers do things differently, so just testing it myself may not work, I do not have every compiler. However, I edited my question to add a bit more meat.Theophilus
There are some sequences of characters that make no sense in C. This is one of them. Learn how to live with it.Ashlynashman
Your compiler accepts --++foo; ? What compiler is that ?Stepmother
Visual Studio 2010 express (although my coworker just let me know he did it in C++ so that changes things)Theophilus
This question has changed enough, I will create a new question about the C++ part.Theophilus
You are not even permitted to increment/decrement a variable more than once between the same two sequence points. For example, (++i * 0) + ++i is invalid. Although it might compile to who-knows-what.Clinkscales
H
8

No, it is not valid because the result of the increment / decrement operators is not a lvalue.

EDIT: the OP edited his question by adding two more examples . So here we go, for the same reason:

--++foo;
--foo++;
foo--++;

are all invalid expression statements because the result of increment / decrement operators is not a lvalue. A compiler could extend the language and accepts these expressions, but a strictly conforming program cannot contain any of these expressions.

Hippopotamus answered 26/7, 2012 at 23:2 Comment(7)
Actually, the result of pre-increment is a lValue.Darnall
@RichardJ.RossIII ++E is equivalent to E+=1 and the result of simple assignment and compound assignment is not a lvalue (C99, 6.5.16p3) "An assignment expression has the value of the left operand after the assignment, but is not an lvalue."Hippopotamus
The confusion comes in part because in C++ an assignment expresion is an lvalue, and so is the prefix increment and decrement. But as this question is tagged C, then @Hippopotamus is right.Trapeze
Yup sorry, I created a new question to address C++, but leaving the part in here (with clarification) to make this answer continue to make sense.Theophilus
C 2011, draft n1256, 6.5.16 3: “An assignment expression has the value of the left operand after the assignment, but is not an lvalue.” 6.5.3.1 2: “The expression ++E is equivalent to (E+=1).”Rugg
Of course, depending on your compiler, monstrosities like a=a-----a; are legal.Candlelight
Note though that the answer to the question title is that you can legally have an increment and decrement of the same variable in the same statement, as long as you have an intervening sequence point: for example, foo++ && foo--; is legal.Akers
S
0

As this is a C expression it should follow some cascading evaluation steps to find out result.

your expressions are

--++foo;

ok lets evaluate this first step:-

++foo;

must be evaluated to proceed

it is some how pre increment so foo become foo+1 .

then assignment happen

that is this evaluate to foo=foo+1;

and value of foo return for assignment to a variable(if nothing present then ignored).

so now our expression is like below

--(++foo); evaluated to

--(a constant value);//result of foo+1

again the statement evaluated like

a constant value=constant_valu+1;//right part is ok but left part is not a variable

as left side of assignment operator is not a variablle and raise an error.

so now no valid operand for -- /decrement operator So lvalue required error

Suh answered 27/7, 2012 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.