conditional operator :? with function calls

This is a discussion on conditional operator :? with function calls within the C Programming forums, part of the General Programming Boards category; consider the statement : Code: i >= 0 ? positive_case() : negative_case(); Does :? need to call and evaluate both ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    22

    conditional operator :? with function calls

    consider the statement :

    Code:
    i >= 0 ? positive_case() : negative_case();
    Does :? need to call and evaluate both functions to execute, or does it just
    call and evaluate one or the other function according to the conditional expression?

    It's confusing me because I don't know how function calls are placed in the
    hierarchy of order of operations. If you consider the absolute value of integer i in this statement :

    Code:
    abs_i = i >= 0 ? i : -i;
    It can't be written like this :

    Code:
    i >= 0 ? abs_i = i : abs_i = -i;     /* doesn't work */
    because the assignment operator is of lower precedence than :?. You need
    parenthesis :

    Code:
    i >= 0 ? (abs_i = i) : (abs_i = -i);     /* works */
    which implies that :? must first evaluate all its operands. So, I guess what I'm asking is, is it bad programming practice to use function calls in such an implementation? I know how it behaves on my compiler, but I don't know what standard C guarantees of this.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Does :? need to call and evaluate both functions to execute, or does it just
    call and evaluate one or the other function according to the conditional expression?
    Check it out for yourself:
    Code:
    i >= 0 ? positive_case() : negative_case();
    Code:
     
    int positive_case() { printf("positive case\n") ; return 1 ; } 
    int negative_case() { printf("negative case\n") ; return 0 ; }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    A bit slow...
    Code:
    int positive_case(void)
    {
      printf("Positive case\n");
      return 1;
    }
    int negative_case(void)
    {
      printf("negative case\n");
      return -1;
    }
    
    i >= 0 ? positive_case() : negative_case();
    // equavilent to
      if( i >= 0) {
        positive_case();
      } else {
        negative_case();
      }
    // also equavilent to .
      (i >= 0 ? positive_case : negative_case) ();      // don't try it @ home :D
    Yes it does *NOT* evaluate both operands.
    Last edited by Bayint Naung; 08-10-2010 at 09:16 AM.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    which implies that :? must first evaluate all its operands
    You're treating C as though it was a dynamic scripting language or a dialect of lisp. The compiler is trying to turn complex C expressions into very 'atomic' machine instructions. The compiler has to process the entire expression and evaluate the precedence of operators, in order to turn your long expression into a sequence of very simple operations. So your compiler is, in a sense, evaluating both expressions, but that doesn't mean that the machine code from both expressions will get run.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    @sean, exactly

  6. #6
    Registered User
    Join Date
    Jul 2010
    Posts
    22
    I get it now. Thanks guys.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by Dino View Post
    Check it out for yourself:
    Code:
    i >= 0 ? positive_case() : negative_case();
    Code:
     
    int positive_case() { printf("positive case\n") ; return 1 ; } 
    int negative_case() { printf("negative case\n") ; return 0 ; }
    One can of course do that, but it doesn't guarantee that the outcome is defined by the standard, which could mean that what it does with that code on one compiler it doesn't do on another.

    Fortunately, I myself have asked this very question before on another forum and can tell you that it is guaranteed by the standard that only one of the last two arguments is evaluated.
    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"

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    ? : is just like any other if-else statement combination. If the first one is true, the second one isn't evaluated.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-16-2008, 02:49 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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