Thread: Comparing integers

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is weird, I tested this on MSVC8 at /W4 and it compiled without any warnings or errors:
    Code:
    #include <iostream>
    #include <vector>
    
    int main()
    {
        std::vector<int> numbers(10);
        std::cout << (-1 < numbers.size() ? "yes" : "no") << std::endl;
    }
    The MinGW port of g++ 3.4.5 reports a warning with -W.
    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

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Heh, seems we've found a dent in the armor. The compiler seems to ignore signed and unsigned comparison when one number is a "magic number", but if they're variables, it warns:

    Code:
    std::vector<int> numbers(10);
    int32_t t1 = -1;
    uint32_t t2 = numbers.size();
    std::cout << (t1 < t2 ? "yes" : "no") << std::endl; // Warning
    std::cout << ((signed)-1 < (unsigned)2); // No warning
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <iostream>
    #include <vector>
    
    int main ()
    {
       std::vector<int> numbers(10);
       int32_t t1 = -1;
       uint32_t t2 = numbers.size();
       std::cout << (t1 < t2 ? "yes" : "no") << std::endl; // Warning
       std::cout << ((signed)-1 < (unsigned)2); // No warning
       return 0;
    }
    
    /* my output
    no
    0
    */
    main.cpp
    ****** {BD Software Proxy c++ v3.43a for gcc} STL Message Decryption is Off ******
    main.cpp: In function `int main()':
    main.cpp:9: warning: comparison between signed and unsigned integer expressions
    main.cpp:10: warning: comparison between signed and unsigned integer expressions
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  3. Comparing two sets of integers
    By bertazoid in forum C Programming
    Replies: 10
    Last Post: 11-26-2008, 07:37 AM
  4. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM