Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ternary operator

Reply
Thread Tools

ternary operator

 
 
novickivan@gmail.com
Guest
Posts: n/a
 
      07-02-2010
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?

Cheers,
Ivan Novick
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      07-02-2010
In article <59f5a3b1-6dce-4382-a82d->,
<> wrote:
>Hello,
>
>What does this print:
>
>printf("%d\n", 3 ?: 4);


CLC answer: A syntax error during compilation.

Real answer: It is a gcc extension.

--
> No, I haven't, that's why I'm asking questions. If you won't help me,
> why don't you just go find your lost manhood elsewhere.


CLC in a nutshell.

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-02-2010
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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-02-2010
Eric Sosman <> writes:

> On 7/2/2010 10:47 AM, wrote:

<snip>
>> printf("%d\n", 3 ?: 4);

<snip>
> 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.


gcc needs -pedantic to turn off its extensions (such as the one being
discussed). This may depend on the gcc version, but I think this is how
it has been for some time.

--
Ben.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function call selection using ternary operator marco_segurini C++ 4 09-21-2004 01:37 PM
ternary operator and ostreams Roger Leigh C++ 6 01-19-2004 07:02 PM
Union, ternary operator, Macro, printf don't cooperate for me. Help? Paul E Johnson C Programming 2 10-17-2003 06:41 AM
union, ternary operator, and C. What a mess! Paul E Johnson C Programming 3 10-17-2003 03:44 AM
ternary operator error Jacob Java 12 07-02-2003 07:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57