Thread: ternary operation as confusing as quadruple bypass

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    ternary operation as confusing as quadruple bypass

    As my fledgling programmer brain has finally started to grasp pointers and the like, the nuances of C syntax are beginning really to intrigue (if baffle) me.

    My question:
    While fiddling with the dreaded ternary operator, I figured out that the following works
    Code:
    int x=2;
    (x==2)?printf("yay!\n"),printf("Whee"):printf("boo");
    I'm not wildly surprised that some variation of the above worked, but a few questions about the fact that it does: I thought the ternary operator evaluated the second and third operands as expressions, which function calls are not...right?

    Also, why don't these calls need semi-colons; does that semicolon in the end in fact get pasted onto the end of the expression? (I think that would be a cute nuance of the operator. (I also think it's cool that
    Code:
    printf("foo"),printf("foo2");
    works. Why don't textbooks mention this? Or is it particular to my compiler?))
    In fact, maybe it's just a highly unportable expression that happens to work on my compiler, and these questions have no one answer. Thoughts?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by forestprimeval
    I thought the ternary operator evaluated the second and third operands as expressions, which function calls are not...right?
    A function call is also an expression.

    Quote Originally Posted by forestprimeval
    Also, why don't these calls need semi-colons; does that semicolon in the end in fact get pasted onto the end of the expression?
    The semi-colon at the end marks the end of the statement.

    Quote Originally Posted by forestprimeval
    I also think it's cool that
    Code:
    printf("foo"),printf("foo2");
    works. Why don't textbooks mention this? Or is it particular to my compiler?
    That is the use of the comma operator. While it may look "cool", overuse can make your code harder to read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You think that's freaky? Check out this:
    Code:
    int foo(int x, int y) {return x * 2 + y;}
    int bar(int x, int y) {return y * 3 + x;}
    
    int main(void)
    {
        int a = 4, b = 5;
        int flag = 1;
        int result = (flag ? foo : bar)(a, b);
    }
    Don't worry, you wont be the only one who didn't know you could do that!

    You can put all sorts of stuff within the ternary operator's true and false conditions. void is an expression type too.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    @iMalc
    That is insanely awesome.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple ternary operation baffling me
    By alter.ego in forum C Programming
    Replies: 4
    Last Post: 02-20-2012, 08:26 AM
  2. What is a quadruple-precision integer package?
    By Gerling in forum C Programming
    Replies: 2
    Last Post: 10-27-2010, 09:50 AM
  3. ternary operation with compound instruction
    By byfreak in forum C Programming
    Replies: 6
    Last Post: 07-01-2008, 03:45 AM
  4. Can OS bypass the BIOS?
    By siavoshkc in forum Tech Board
    Replies: 31
    Last Post: 08-09-2006, 10:53 AM
  5. ternary operation
    By lambs4 in forum C Programming
    Replies: 10
    Last Post: 12-30-2002, 08:29 AM

Tags for this Thread