For context I am fairly new to programming after returning back to it after programming a number of years ago... I was reading Bjarne's intro programming with C++ and was wondering if alphabetical string characters are assigned numerical values for the purpose of comparison. For instance in this example from the book Bjarne shows that the strings are compared through analyzing the first letter of the input for the strings. From what I gather the language has been coded to essentially give the string characters a value of what I am assuming is 1-26 corresponding to the standard US alphabet to compare the letters.

For instance:

int main()
    cout << "Please enter two names\n ";
    string first;
    string second;
    cin >> first >> second;
    if (first<second) 
        cout << first <<"is alphabetically before"<< second <<'\n ' ;
    return 0;
  • 2
    Look up ASCII, EBCDIC, and the source code for stdlib strcmp. Jul 30, 2015 at 23:30
  • 2
    FYI, we have 26 letters in the alphabet now :-) Jul 31, 2015 at 0:19

2 Answers 2

if (first<second) 

In this case, there is something different involved here, the operator< from std::basic_string.

and was wondering if alphabetical string characters are assigned numerical values for the purpose of comparison.

No, not really. You don't assign a number to a character just for comparison. A computer does not know anything about characters, a computer just knows numbers. What number represents what letter is defined in a charset, ASCII for example is a widely known one. However, a table alone doesn't help the computer either, since it still does not know what a character is and what it should do with it, therefore, at the end, it is the font which makes the number 65 look like a 'A'. A font is just a file with a list of numbers and images (bitmap, vector, ...) and the computer just draws the image which belongs to the number 65, so as I already said, the computer knows nothing about characters, it just draws images.


It depends on the programming language you use and on the compiler that you are using.

The C and C++ languages define very little. All that you can rely on - from the language definition - is that the digits '0' to '9' have sequential values. You cannot even portably rely on 'a' to 'z' having sequential values, not from the C and C++ language definition alone.

A specific implementation will usually give you more guarantees. For example, most C and C++ implementations guarantee that your character set is a superset of ASCII (google for ASCII for more information). Another example, the compilers shipping with Xcode guarantee that single byte C and C++ strings contain characters in UTF-8 format (again, use google). Languages like Java give much more guarantees as part of the language definition.

But as a note, using the codes is absolutely not enough for meaningful comparison and ordering. Useful ordering is complicated, and you just hope that the operating system supplies code that helps you, because on your own you have little chance. For example, naïve code will sort "Bag" < "Coat" < "anorak", and "Chapter 1" < "Chapter 10" < "Chapter 19" < "Chapter 2". Not what a user will find helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.