Solved

Believe it or not, legal, or not?

Posted on 2004-09-22
3
275 Views
Last Modified: 2010-04-15
++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
Comment
Question by:SaMuEl
3 Comments
 
LVL 23

Accepted Solution

by:
Mysidia earned 20 total points
Comment Utility
> ++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
 
LVL 7

Assisted Solution

by:aib_42
aib_42 earned 20 total points
Comment Utility
++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
 
LVL 22

Assisted Solution

by:NovaDenizen
NovaDenizen earned 20 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now