ternary operator

Discussion in 'C Programming' started by novickivan@gmail.com, Jul 2, 2010.

  1. Guest

    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
    , Jul 2, 2010
    #1
    1. Advertising

  2. In article <>,
    <> 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.
    Kenny McCormack, Jul 2, 2010
    #2
    1. Advertising

  3. Eric Sosman Guest

    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
    Eric Sosman, Jul 2, 2010
    #3
  4. 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.
    Ben Bacarisse, Jul 2, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jacob

    ternary operator error

    Jacob, Jun 30, 2003, in forum: Java
    Replies:
    12
    Views:
    4,708
    pete kirkham
    Jul 2, 2003
  2. Roger Leigh

    ternary operator and ostreams

    Roger Leigh, Jan 16, 2004, in forum: C++
    Replies:
    6
    Views:
    648
    Roger Leigh
    Jan 19, 2004
  3. marco_segurini
    Replies:
    4
    Views:
    792
    Dan Cernat
    Sep 21, 2004
  4. Paul E Johnson

    union, ternary operator, and C. What a mess!

    Paul E Johnson, Oct 17, 2003, in forum: C Programming
    Replies:
    3
    Views:
    450
    Ed Morton
    Oct 17, 2003
  5. Paul E Johnson
    Replies:
    2
    Views:
    565
    Christian Bau
    Oct 17, 2003
Loading...

Share This Page