Shao Miller
5/9/2011 6:44:00 PM
On 5/9/2011 1:14 PM, Fred wrote:
> On May 9, 10:19 am, Keith Thompson<ks...@mib.org> wrote:
>> As far as the C language is concerned, a function returning int is
>> simply incompatible with a function returning void, and the behavior of
>> a program that calls one such function as if it were the other is
>> undefined.
>>
>
> So you are saying that the statement
> printf( "Hello, World!\n" );
> provokes undefined behavior, since I am calling it
> as if it were a function returning void rather than
> int n;
> n = printf( "Hello, World!\n" );
> where I am calling it as a function returning an int?
I don't think he's saying that.
I believe that the expression for the called function, 'printf' in this
instance, is the same regardless of what you are doing with the result
of the function call. And in your former case, I think that you simply
have a "void expression," which is evaluated.
The same as:
int n = 3;
n;
You might chop it up thusly:
[ [ [printf] [ [(] ["Hello, world!\n"] [)] ] ] [;] ]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
and:
[ [ [n] [=] [ [printf] [ [(] ["Hello, world!\n"] [)] ] ] ] [;] ]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The "outer" expression for the assignment of 'n' should not influence
the "inner" expression for the function call; that expression is a
sub-expression in its own right, if I understand correctly. :)