Thread: Comparing integers

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Comparing integers

    I don't know why (-1 < s.size()) evaluates to false, when -1 is less than 3.

    Code:
    int main()
    {
    	string s = "abc";
    	
    	cout << s.size() << '\n';
    
    	if (-1 < s.size()) {
    		cout << "yes";
    	} else {
    		cout << "no";
    	}
    
    	keep_window_open();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    s.size() returns a string::size_type, which is indirectly a typedef for size_t, which is an unsigned integer type (typically itself a typedef for unsigned int).

    So, -1 < s.size() is a comparison between a signed and unsigned integer. The conversion of -1 to an unsigned integer resulted in a number larger than what s.size() returned.
    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
    Jan 2008
    Posts
    65
    Is there an easy way to fix this?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is there an easy way to fix this?
    Yes, just write:
    Code:
    int main()
    {
    	string s = "abc";
    	
    	cout << s.size() << '\n';
    
    	cout << "yes";
    
    	keep_window_open();
    }
    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

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    laserlight: haha lolo!
    really strange, one unsigned other signed and they promote the signed to unsigned?
    actually i think you would get a warning, which will ask for a cast, and this will fix the problem, before it annoys you.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it should indeed prompt a warning. When comparing integers, make sure both are of the same type.
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Yes, it should indeed prompt a warning. When comparing integers, make sure both are of the same type.
    And at least g++ gives a warning when using -Wall. I'm pretty sure MSVC is the same.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    I'm pretty sure MSVC is the same.
    So it does, on default warning levels even.
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    So it does, on default warning levels even.
    Yes, gcc/g++ only needs -W too - I just didn't actually check at which warning level it appears. I normally use -Wall.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    I'm using Visual C++ and it didn't warn me. I didn't change any warning levels either.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All Visual Studio versions I've used has warned on signed/unsigned comparison, even on default warning levels. What version are you using?
    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.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    65
    Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In project settings, under C/C++, make sure "Warning level" is set to 4.
    In the output window, check for warnings in the output.
    If using the Task List, make sure the Warning button isn't deselected (thus silencing the warnings).

    You should get warnings about it.
    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.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jw232 View Post
    Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM
    You probably have "warnings" turned down then - check the setting, and set it one or two levels higher (W3 is a good setting, W4 tends to give slightly stupid warnings).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But W4 is the only one that can warn about potentially uninitialized variables and unused variables and the use of non-standard extensions, so I suggest leaving it on W4 (level 4).
    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.

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