F
Fredrik Tolf
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
#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