[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Believe it or not, legal, or not?

++i++
Legal or not?
My compiler doesn't like it, but I've seen it published that it is, anyone know what it says according to ANSI-C.

how about this
i+++++j
Compiler doesn't like this either, I can see why
if I was the compiler I'd see it as (i++)++ + j, however could also be interpreted as i++ + j++.
Hmm, whitespace shouldn't be an issue in C, but which to choose?

i+++ ++j
This way compiled!


Finally,
I think I figured this one out, but why does this
#include <stdio.h>

main()
{
    int i=0;
    int x=0;
    x = i++ + i++ + i++;
    printf("i = %d  x = %d \n",i,x);
    puts("x = i++ + i++ + i++");
    printf("i = %d  x = %d \n",i,x);

    i=0;
    x=0;    
    printf("\ni = %d  x = %d \n",i,x);
    x = i++ + ++i + i++;
    puts("x = i++ + ++i + i++");  
    printf("i = %d  x = %d \n",i,x);
   
    i=0;
    x=0;  
    printf("\ni = %d  x = %d \n",i,x);
    x = ++i + ++i + ++i;
    puts("x = ++i + ++i + ++i");  
    printf("i = %d  x = %d \n",i,x);
   
    getchar();
}    

give me

OUTPUT
i = 0  x = 0
x = i++ + i++ + i++
i = 3  x = 0

i = 0  x = 0
x = i++ + ++i + i++
i = 3  x = 3

i = 0  x = 0
x = ++i + ++i + ++i
i = 3  x = 7

and not...
i = 0  x = 0
x = i++ + i++ + i++
i = 3  x = 3

i = 0  x = 0
x = i++ + ++i + i++
i = 3  x = 4

i = 0  x = 0
x = ++i + ++i + ++i
i = 3  x = 6

Well... I'll tell you the conclusion I came to in a while, but perhaps someone could clarify in the meantime.
0
SaMuEl
Asked:
SaMuEl
3 Solutions
 
MysidiaCommented:
> ++i++
> Legal or not?

No.  If you want to see what the standard says about it, check out the section on constraints for prefix
and postfix operators 6.5.3.1 (looking at ISO 9899:1999 E).  Most notably: the prefix or postfix operator can only be applied to a modifiable value, but does not return a modifiable value.

(++i)++   is not legitimate  nor is  ++(i++)  

because neither ++i  nor i++  yields a modifiable value, the ++ operator cannot
be applied to it.

As for code that looks like:      x = i++ + ++i + i++;

The result is left undefined.  The order of evaluation for the ++ and -- operators is really
up to the specific compiler/runtime environment you're using.

just like the values passed to f by  f(i - 1, ++i)   are undefined... the x expression is



0
 
aib_42Commented:
++i++

Illegal. Regardless of the order, neither (i++) nor (++i) are legal lvalues for the prefix or the postfix incrementation operators, respectively.


i+++++j

Legal only if interpreted as (i++)+(++j). I wouldn't know how it would be interpreted based on operator precedence. I wouldn't read any such code if it didn't explicitly use parantheses.


x = i++ + i++ + i++;
or
x = i++ + ++i + i++;
or
x = ++i + ++i + ++i;
or
x = (i++) + (i++) + (i++);
or
x = (i++) + (++i) + (i++);
or
x = (++i) + (++i) + (++i);

All illegal. The standard states that, if an object (e.g. int i) is accessed multiple times in between sequence points, it may be accessed to be modified _at_most_ once, i.e. all other accesses must be for the purposes of getting its value.

Since these result in undefined behaviour, there is no telling what the output will be. (Or if there will be any output at all, since theoretically they can turn your computer into a pink banana. :)
0
 
NovaDenizenCommented:
The rule is that whenever a long sequence of operator characters is present, the parser is supposed to scoop up the largest string that makes up an operator, even if it makes the expression as a whole invalid.
i+++++j will always be parsed as ((i ++) ++) + j, which is illegal.
i+++ ++j is parsed as (i++) + (++j), which is fine.
---j parses as (-- (- j)), which is illegal.
- --j parses as (- (--j)), which is fine.
++3 parses as (++ 3) which is illegal
+ + 3 parses as (+ (+ 3)) which is fine.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now