Comparing integers

This is a discussion on Comparing integers within the C++ Programming forums, part of the General Programming Boards category; This is weird, I tested this on MSVC8 at /W4 and it compiled without any warnings or errors: Code: #include ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    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.
    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

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    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,006
    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.*

Page 2 of 2 FirstFirst 12
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, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Comparing two sets of integers
    By bertazoid in forum C Programming
    Replies: 10
    Last Post: 11-26-2008, 06: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

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