Thread: Simple ternary operation baffling me

  1. #1
    Registered User
    Join Date
    Apr 2011

    Simple ternary operation baffling me

    #include <stdio.h>
    #include <stdlib.h>
    inline int min(int x, int y) { return x = y ? x : y; }
    int main(int argc, char** argv) {
        int x = 1;
        int y = 15;
        printf("x=%d,y=%d\n", x, y);
        printf("min(x,y)=%d\n", min(x, y));
        return (0);
    While Googling for some stuff I chanced upon above code. My idea of Ternary operation is in slight doubt. In code below I don't see any comparison.

    { return x <= y ? x : y; }
    I would've written something like this:

    {return x = (x<y) ? x:y;}
    Hope I explained myself correctly.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    The comparison is x <= y. If you really saw x = y, then it is probably just a typographical error.
    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
    Registered User
    Join Date
    Oct 2011
    In C, in order to compare two values, you must use the "==" operator. The "=" operator is used for assigning values.

    Then the second code has comparison, the literal translation would be : "is x less than or equal to y? if yes, return x, otherwise return y".

    Hope that helps.
    HomePort : A C Web Service API for heterogeneous home automation systems

  4. #4
    Registered User
    Join Date
    Apr 2011
    @laserlight @Tibo-88: Thanks for quick revert. It was not typo; I was flummoxed seeing expression "x<=y" in ternary operation. But thanks for clarifying.
    Last edited by alter.ego; 02-20-2012 at 03:46 AM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Using the '<=' operator is confusing in this case IMO because the function name is min() and the result is the same regardless, because if 'x == y' it does not matter which one is returned.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to change this simple opeation into power operation..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 12-20-2008, 03:17 PM
  2. ternary operation with compound instruction
    By byfreak in forum C Programming
    Replies: 6
    Last Post: 07-01-2008, 03:45 AM
  3. ternary operator
    By abhijith gopal in forum C Programming
    Replies: 37
    Last Post: 07-10-2006, 11:58 AM
  4. ternary operation
    By lambs4 in forum C Programming
    Replies: 10
    Last Post: 12-30-2002, 08:29 AM
  5. Replies: 5
    Last Post: 12-17-2001, 11:59 AM