Comparing integers

This is a discussion on Comparing integers within the C++ Programming forums, part of the General Programming Boards category; I don't know why (-1 < s.size()) evaluates to false, when -1 is less than 3. Code: int main() { ...

  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
    21,450
    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.
    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
    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
    21,450
    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();
    }
    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

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    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.

Page 1 of 2 12 LastLast
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