Thread: ? operator...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116

    ? operator...

    The "?" conditional operator, for those that aren't aware, acts very much like a an if...else statement:
    Code:
    z = (x>y) ? x : y;
    is similar to
    Code:
    if (x > y)
        z = x;
        else 
            z = y;
    My question is: how often are you going to see this and, other then less code written, am I missing out on some amazing functionality by not using it? It just doesn't seem like something people would use too often.

    thanks.
    Last edited by MikeyIckey; 05-24-2010 at 05:52 PM. Reason: correction needed for code used

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by MikeyIckey View Post
    My question is: how often are you going to see this and, other then less code written, am I missing out on some amazing functionality by not using it? It just doesn't seem like something people would use too often..
    You are (almost) asking the wrong questions. It doesn't matter nearly so much how often this is used, or how many people use it, but how it can help you write nice, clear, compact, comprehensible code. It is an elegant way of branching if there are two choices. Of course you could use conditionals within conditionals, and quzah might show you how to do that , but I guess if you have more than two choices you would then resort to an if/else construct, or possibly a case/switch.
    Last edited by kermit; 05-24-2010 at 06:46 PM.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    No, it's an interesting way to branch code. I just learned about it about (even though I think it's been in the C spec since K&R ansi c) so I was just wondering how often people see it, is it considered bad or good to use it, pros, cons, that kind of thing. But thanks for the reply.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    It is a compact way of writing; whether it is actually clearer is a matter of opinion and how its used..
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Sometimes ternary operator is useful.
    eg .
    Code:
     #define MAX(a,b)   ( (a) > (b) ? (a) : (b) )
          ret = f(x > 0 ? a : b, y < 0 ? c : d);   /* with if else it's troublesome */
       //  or even
         *( f() ? &x : &y) = 10;                    /* odd! */

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    OK, Mr. Naung, you've convinced me. That's a pretty interesting (and useful) use of the ? operator!
    Thanks for everyone's replies.
    -M

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Its only purpose is to allow conditional evaluation in an expression context. An if-statement is, well, a statement. Not an expression. So if you need conditional evaluation inside a macro, for instance, if-statements may not be a possibility.

    That said, it does allow compact expression of alternatives in the following way:

    Code:
    x = condition1 ? a
      : condition2 ? b
      : condition3 ? c
      : d;
    If used as above and indented as above, I don't mind seeing it. Most other uses just make me angry.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    That's some pretty impressive functionality, actually.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    As an expression, it's also useful in function arguments :

    printf ("Found %d item%s\n", item_count, item_count == 1 ? "" : "s");

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by MikeyIckey View Post
    That's some pretty impressive functionality, actually.
    It seems that way, but outside of the occasional a>b? a : b kind of thing if-else if is a lot more readable. You have to be really, really lazy to use it in place of "if-else if" as a policy.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by KCfromNC View Post
    As an expression, it's also useful in function arguments :

    printf ("Found %d item%s\n", item_count, item_count == 1 ? "" : "s");
    Nice!

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by Subsonics View Post
    Nice!
    yeah, i liked that too.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by MK27 View Post
    It seems that way, but outside of the occasional a>b? a : b kind of thing if-else if is a lot more readable. You have to be really, really lazy to use it in place of "if-else if" as a policy.
    so, for pure readability, you're not a fan?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

Tags for this Thread