Like Tree3Likes
  • 3 Post By iMalc

ternary operation as confusing as quadruple bypass

This is a discussion on ternary operation as confusing as quadruple bypass within the C Programming forums, part of the General Programming Boards category; As my fledgling programmer brain has finally started to grasp pointers and the like, the nuances of C syntax are ...

  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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,304
    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.
    Click_here, ZuK and stahta01 like this.
    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,367
    @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, 07: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, 07:29 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21