comparing "1" and "0"

This is a discussion on comparing "1" and "0" within the C++ Programming forums, part of the General Programming Boards category; Apologies if this seems like a silly q, but why do the statements cout << ("0" < "1") cout << ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Question comparing "1" and "0"

    Apologies if this seems like a silly q, but why do the statements
    cout << ("0" < "1")
    cout << ("1" < "0") both return true?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    Because you're comparing pointers (to strings) rather than strings.


    Try with
    Code:
    #include <string>
    std::string zero = "0";
    std::string one = "1";
    cout << (zero < one);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    similarily
    Code:
    	cout << (*"0" < *"1");
    	cout << (*"1" < *"0");

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,112
    Quote Originally Posted by MadCow257
    similarily
    Code:
    	cout << (*"0" < *"1");
    	cout << (*"1" < *"0");
    Ah, but what about (*"10" < *"11") ?
    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
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Thankyou all for such quick replies!
    I'm still unclear on exactly how my example was working though.. I'm guessing that I was comparing pointers to both strings and so whatever I have on the left of the operator, the value of that pointer will always be less than that of the right pointer because of the order im putting them in memory? is this right or just rubbish?

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by rayman
    Thankyou all for such quick replies!
    I'm still unclear on exactly how my example was working though.. I'm guessing that I was comparing pointers to both strings and so whatever I have on the left of the operator, the value of that pointer will always be less than that of the right pointer because of the order im putting them in memory? is this right or just rubbish?
    You are correct in that you've compared the memory addresses of the strings.

    Whether the left or right string gets put in memory at a lower-valued memory location depends on the compiler and its behavior is generally unpredictable.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm still unclear on exactly how my example was working though.
    Whenever a string literal is encounted in a program(i.e. something between double quotes), the program slaps a '\0' character on the end of the string literal, stores it in memory somewhere, and returns the address. So, the line:

    cout << ("0" < "1")

    becomes:

    cout<<(address of str1 < address of str2);
    Last edited by 7stud; 03-17-2006 at 05:35 PM.

  8. #8
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    I'm kind of confused too

    What's different with this

    (*"10" < *"11")

    and this

    (*"0" < *"1")

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    *"10";

    <==>

    const char* const c = "10";
    *c;

    <==>

    const char* const c = "10";
    c[0];

    <==>

    '1'

    not 10
    Last edited by Magos; 03-17-2006 at 07:16 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    I think you mean '1', not 1.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm kind of confused too

    What's different with this

    (*"10" < *"11")

    and this

    (*"0" < *"1")
    Magos??? How about this:
    Code:
    const char* ptr1 = "123456789";
    const char* ptr2 = "1";
    
    cout<<*ptr1<<endl;  //'1'
    cout<<*ptr2<<endl;  //'1'
    Last edited by 7stud; 03-18-2006 at 04:24 AM.

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by rayman
    Apologies if this seems like a silly q, but why do the statements
    cout << ("0" < "1")
    cout << ("1" < "0") both return true?
    If that really happens with your compiler then I think it has to do that your compiler doesn't optimize string literals. It propably creates a char array for every literal in you program and doesn't check for duplicates.
    g++ does
    Code:
    # include <iostream>
    # include <iomanip>
    
    using namespace std;
    
    int main() {
        cout << boolalpha;
        cout << "1 < 0 = " << ( "1" < "0" ) << endl;
        cout << "0 < 1 = " << ( "0" < "1" ) << endl;
        return 0;
    }
    my output
    Code:
    1 < 0 = true
    0 < 1 = false
    As far as I know this is implementation defined behavour.
    Kurt

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    As far as I know this is implementation defined behavour.
    It is. Compilers are free to store string literals anywhere, in any order.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If you want to compare string literals properly, use strcmp() or strings:
    Code:
    if(strcmp("0", "1") < 0) { /* 0 is < 1 */ }
    string one = "1", two = "2";
    if(one < two) { /* 1 is < 2 */ }
    if(string("11") < string("7")) { /* 11 is < 7 */ }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. concatenating a char * without "\0"
    By sameerc in forum C++ Programming
    Replies: 5
    Last Post: 05-10-2005, 08:10 AM
  2. solving "0" of polynomials
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2001, 10:55 PM

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