Solved

Believe it or not, legal, or not?

Posted on 2004-09-22
3
280 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
ID: 12130859
> ++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
ID: 12130897
++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
ID: 12135912
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
voltage to force translation ? 8 99
why "." vs "->" 23 120
change colour of repeater control in asp.net c# 7 84
smtp c source code 7 67
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

813 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

12 Experts available now in Live!

Get 1:1 Help Now