preincrement

  • Thread starter prasoonthegreat
  • Start date
P

prasoonthegreat

int main(void ){
int i=1;
++i =5;
return 0;
}

Does the above code show Undefined Behaviour???
The value of i is modified just once between two sequence
points.....isn't it????

Please correct me...if I am wrong
 
B

BartC

int main(void ){
int i=1;
++i =5;
return 0;
}

Does the above code show Undefined Behaviour???
The value of i is modified just once between two sequence
points.....isn't it????

Try compiling it.
 
E

Eric Sosman

int main(void ){
int i=1;
++i =5;
return 0;
}

Does the above code show Undefined Behaviour???

A diagnostic is required because `++i' cannot be the
target of an assignment (for the same reason `printf("!\n")'
cannot be the target of an assignment).

After issuing the diagnostic, the compiler can reject
the erroneous code outright, or it can try to make sense of
it and do something anyhow. In the latter case, the Standard
says nothing about what the resulting program might do, but
I'm not entirely sure whether this makes it Undefined
Behavior or Implementation-Defined Behavior.
The value of i is modified just once between two sequence
points.....isn't it????

There's no way to tell. If the compiler applies some
kind of correction and somehow runs the altered program,
we don't know what that altered program might look like or
what it might do. The program as it stands is not C.
 
B

Barry Schwarz

int main(void ){
int i=1;
++i =5;
return 0;
}

Does the above code show Undefined Behaviour???
The value of i is modified just once between two sequence
points.....isn't it????

Ignoring the syntax error, how many sequence points are there in the
statement
++i = 5;

How many times is i modified?

(Hint: 1 and 2, respectively.)
 
K

Keith Thompson

Eric Sosman said:
A diagnostic is required because `++i' cannot be the
target of an assignment (for the same reason `printf("!\n")'
cannot be the target of an assignment).

After issuing the diagnostic, the compiler can reject
the erroneous code outright, or it can try to make sense of
it and do something anyhow. In the latter case, the Standard
says nothing about what the resulting program might do, but
I'm not entirely sure whether this makes it Undefined
Behavior or Implementation-Defined Behavior.

It's undefined. As you say, the standard says nothing about what the
resulting program might do; that's just what "undefined behavior"
means.
There's no way to tell. If the compiler applies some
kind of correction and somehow runs the altered program,
we don't know what that altered program might look like or
what it might do. The program as it stands is not C.

But it might be C++. Google "C++ FAQ" or post to
comp.lang.c++{,.moderated} for more information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,886
Members
47,419
Latest member
ArturoBres

Latest Threads

Top