comp.lang.c
[Top] [All Lists]

Re: Undefined behaviour in expressions

Subject: Re: Undefined behaviour in expressions
From: "Dann Corbit"
Date: Tue, 1 Apr 2008 12:06:53 -0700
Newsgroups: comp.lang.c

"Ioannis Vranos" <ivranos@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message 
news:fsu0mu$257t$1@xxxxxxxxxxxxxxxxxxxxxx
> ==> C95:
>
>
> I read in some text that we get undefined behaviour in expressions when
> we modify a variable more than once in the expression. So I wonder if
> the following codes have undefined behaviour:
>
>
> 1.
>
> #include <stdio.h>
>
>
> int main()
> {
> int x=1;
>
>        /* implementation-dependent result? */
> int y= x++/x--;

Undefined behavior.  The variable x is modified more than once without any 
intervening sequence point.

>
> printf("%d\n", y);
>
> return 0;
> }
>
>
>
> 2. I think the following is well defined, since the expression is
> evaluated from left to right, and y gets assigned the value 1.
>
> #include <stdio.h>
>
>
> int main()
> {
> int x=1;
>
> int y= (x++, x--);

The comma is a sequence point.  The behavior is well defined.

> printf("%d\n", y);
>
> return 0;
> }



-- 
Posted via a free Usenet account from http://www.teranews.com


<Prev in Thread] Current Thread [Next in Thread>