Compound statements in expressions
From: Fredrik Tolf (fredrik_at_dolda2000.com)
Date: 09/30/04
- Next message: Marcus Jacobs: "Pointer Usage"
- Previous message: CBFalconer: "Re: Trying to understand pointers for function paramaters"
- Next in thread: Keith Thompson: "Re: Compound statements in expressions"
- Reply: Keith Thompson: "Re: Compound statements in expressions"
- Reply: Dag Viken: "Re: Compound statements in expressions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 30 Sep 2004 04:51:34 +0200
Take a look at this C snippet:
#include <stdio.h>
int test(int *var)
{
return(*var += 5);
}
int main(void)
{
int var;
var = 1;
printf("%i %i %i\n", ({var += 4; var;}),
(printf("%i\n", test(&var)), var),
(var += 10, var -= 4));
}
When compiled with GCC 3.3, it outputs the following when run:
6
10 16 16
Now I'm wondering - Are these types of constructs allowed by the C
standard, or are they GCC extensions? If they are standardized, what is
the standardized semantics. That is, in which order are they intended to
be executed, what's the difference between "(var += 4, var)" and "({var
+=4; var;})", and what does "(expr1, expr2, expr3)" really mean when
it's not an argument list to a function.
Also, why must I write "({var += 4; var;})" - Why can't I skip the outer
parentheses?
Thanks for your attention!
Fredrik Tolf
- Next message: Marcus Jacobs: "Pointer Usage"
- Previous message: CBFalconer: "Re: Trying to understand pointers for function paramaters"
- Next in thread: Keith Thompson: "Re: Compound statements in expressions"
- Reply: Keith Thompson: "Re: Compound statements in expressions"
- Reply: Dag Viken: "Re: Compound statements in expressions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|