Like Tree1Likes
  • 1 Post By laserlight

Simple ternary operation baffling me

This is a discussion on Simple ternary operation baffling me within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> inline int min(int x, int y) { return x = y ? x : y; ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    55

    Simple ternary operation baffling me

    Code:
    #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.

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

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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    The comparison is x <= y. If you really saw x = y, then it is probably just a typographical error.
    alter.ego likes this.
    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
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    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
    Posts
    55
    @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 02:46 AM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    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, 02: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, 07:29 AM
  5. Replies: 5
    Last Post: 12-17-2001, 10:59 AM

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