On 7/2/2010 10:47 AM,
wrote:
> Hello,
>
> What does this print:
>
> printf("%d\n", 3 ?: 4);
>
> For me it seems to print the number 3. It seems the expression says
> if the first expression is true than use the second expression as the
> result. But if there is no second expression than use the first
> expression.
>
> I don't see this syntax (with the missing second expression)
> referenced in the C language standard, so I was wondering if this is
> official C language syntax?
It is not. A conforming C implementation must issue a
diagnostic message.
You're probably using the popular gcc compiler, whose default
mode compiles a language that isn't exactly C, but "C with extras."
One of those extras is the construct you ask about
: In gcc-dialect,
`x ?: y' is equivalent to `x ? x : y' (except that `x' is evaluated
only once).
To compile standard C with the gcc compiler, use a command-line
flag to specify the C version you intend: `-ansi' or `-std=c99',
usually. Consider using the `-pedantic' flag if your code is clean
enough to withstand it. I also recommend `-Wall -W'.
--
Eric Sosman
lid