Solved

Believe it or not, legal, or not?

Posted on 2004-09-22
3
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

691 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