Thread: Comparing integers the short way

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28

    Comparing integers the short way

    I saw this in a project (simplified of course). Is that a good way to do it or should I use an if statement instead?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    int main()
    {
        int a = 10;
        int b = 20;
    
        int rv = a == b;
        printf("rv=%d\n", rv);
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you really just need to store 0 into a variable if the integers are not equal and 1 if they are equal, go ahead: it is guaranteed to work that way.
    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
    Jun 2012
    Location
    Göteborg
    Posts
    28
    Quote Originally Posted by laserlight View Post
    If you really just need to store 0 into a variable if the integers are not equal and 1 if they are equal, go ahead: it is guaranteed to work that way.
    OK, Thanks for your reply.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Think how you would use this in a real program. What would you do if a == b, or if a != b. In a real program, would this method be more efficient than a simple if( a == b)...?

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Göteborg
    Posts
    28
    I prefer a simple if(a== b). It's just that I found this in one of our project and got a bit puzzled.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by hzcodec View Post
    I prefer a simple if(a== b). It's just that I found this in one of our project and got a bit puzzled.
    I'd be puzzled too! I would love to see this person's code for this and other surprises! ;^)

  7. #7
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by hzcodec View Post
    I prefer a simple if(a== b). It's just that I found this in one of our project and got a bit puzzled.
    It's not really a matter of preference but a matter of what you are actually trying to accomplish in a real program. If all you want to do is assign 0 or 1 to a variable based on the comparison then
    Code:
    x = a == b
    is perfectly acceptable, and generally more efficient than
    Code:
    if (a == b)
        x = 1;
    else
        x = 0;
    (ignoring compiler optimizations that would turn the latter into the former).

    In my opinion, the former code is better, the latter code is amateurish.

    Of course, if you want to do something more than simply set a variable to the result, then you would need the if statement.

  8. #8
    Registered User
    Join Date
    Jan 2017
    Posts
    16
    Quote Originally Posted by algorism View Post
    ... Of course, if you want to do something more than simply set a variable to the result, then you would need the if statement.
    Or the ?: operator

  9. #9
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by TechnoGourmet View Post
    Or the ?: operator
    Which is really just syntactic sugar for an if statement, i.e., it still involves a jump in the code (with the resultant potential cache miss penalty).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Pointer and Integers?
    By M_A_T_T in forum C Programming
    Replies: 4
    Last Post: 03-25-2013, 02:56 PM
  2. comparing integers
    By tosihiro2007 in forum C Programming
    Replies: 2
    Last Post: 03-15-2013, 01:30 PM
  3. Scanning and Printing short integers
    By sackboy127 in forum C Programming
    Replies: 6
    Last Post: 12-10-2012, 06:12 AM
  4. Comparing two sets of integers
    By bertazoid in forum C Programming
    Replies: 10
    Last Post: 11-26-2008, 07:37 AM
  5. Comparing integers
    By jw232 in forum C++ Programming
    Replies: 17
    Last Post: 04-01-2008, 11:36 AM

Tags for this Thread