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

Re: Implementation-defined behaviour

Subject: Re: Implementation-defined behaviour
From: Harald van Dijk
Date: Fri, 28 Mar 2008 22:56:39 +0100
Newsgroups: comp.lang.c

On Fri, 28 Mar 2008 16:44:12 -0500, pete wrote:
> Ioannis Vranos wrote:
>> K&R2 mentions the following:
>> 
>> "printf("%d %d\n", ++n, power(2,n));   /* WRONG */
>> 
>> can produce different results with different compilers, depending on
>> whether n is incremented before power is called".
>> 
>> That's why I call it implementation-defined behaviour.
> 
> It's unspecified behavior.
> The power(2,n) function call, introduces a sequence point that the other
> code examples don't have. This is all about sequence points.

If arguments are evaluated strictly from left to right (nothing wrong 
with that), here are the steps:
- evaluate "%d %d\n"
- evaluate ++n
- evaluate 2
- evaluate n
* call power
* call printf

There is not necessarily any sequence point between the evaluation of ++n 
and the evaluation of n, unless I'm missing something.

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