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?

Ajay
  • 775
  • 5
  • 19
Ashterothi
  • 3,282
  • 1
  • 21
  • 35
  • 6
    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) – Levon Jul 26 '12 at 23:01
  • 1
    The primary question is could this be a legal command. – Ashterothi Jul 26 '12 at 23:01
  • Part one of this question is trial and error. Part two is using your head... – Linus Kleen Jul 26 '12 at 23:03
  • @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. – Levon Jul 26 '12 at 23:05
  • 1
    @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". – Linus Kleen Jul 26 '12 at 23:05
  • 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. – Ashterothi Jul 26 '12 at 23:09
  • There are some sequences of characters that make no sense in C. This is one of them. Learn how to live with it. – wildplasser Jul 26 '12 at 23:13
  • Your compiler accepts `--++foo;` ? What compiler is that ? – cnicutar Jul 26 '12 at 23:17
  • Visual Studio 2010 express (although my coworker just let me know he did it in C++ so that changes things) – Ashterothi Jul 26 '12 at 23:19
  • This question has changed enough, I will create a new question about the C++ part. – Ashterothi Jul 26 '12 at 23:21
  • 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. – Dietrich Epp Jul 26 '12 at 23:23

2 Answers2

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.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 1
    Actually, the result of pre-increment `is` a lValue. – Richard J. Ross III Jul 26 '12 at 23:02
  • 2
    @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."* – ouah Jul 26 '12 at 23:11
  • I'm sorry but this is the right answer, downvoter (@RichardJ.RossIII) please remove the downvote. – ouah Jul 26 '12 at 23:13
  • 2
    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 @ouah is right. – rodrigo Jul 26 '12 at 23:23
  • 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. – Ashterothi Jul 26 '12 at 23:29
  • 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).” – Eric Postpischil Jul 26 '12 at 23:58
  • Of course, depending on your compiler, monstrosities like `a=a-----a;` are legal. – David G Jul 27 '12 at 04:10
  • 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. – caf Jul 27 '12 at 04:26
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

rajesh6115
  • 705
  • 9
  • 21