4.1: Character Data Type and ACII Code

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    4.1: Character Data Type and ACII Code

    In this section we will discuss how to store and process characters in a C++ program.

    In a C++ program, if we want to use a variable a to store a single character, we will need following statement to declare such variable:

    char a;

    where 'char' is a character data type which indicates variable a is a variable that can be used for storing a single character.

    In a C++ program, if we want to assign a single character into a variable, say assign character M into variable a, we may use following statement:

    a = 'M';

    Note: the character that you want to assign to a character variable must be placed within a pair of single quotation marks.

    ASCII Code

    Characters are not limited to upper case or lower case letters. A digit, a punctuation mark, even a blank space is also considered as a character. Characters are also include those special symbols such as $, %, &, +, * and so on.

    Computers use a specific integer to represent each character, such integer is called ASCII code (American Standard Code for Information Interchange). Following table gives the ASCII codes of some of the characters we often use in a C++ program:

    ASCII Code for Commonly Used Characters:

    Characters ASCII Code
    '0' to '9' 48 to 57
    'A' to 'Z' 64 to 90
    'a' to 'z' 97 to 122

    The ASCII code for '$' is 36, '#' is 35, and the ASCII code for a blank space is 32 and so on. If you would like to find out the ASCII code for a specific character, say '%', this is what you can do:

    1. Declare an integer variable
      int a;
    2. Put in a line of code as following:
      a='%';
    3. Using following cout statement to display the ASCII code of '%' on the screen:
      cout<<a<<endl;

    Why do we need to know the ASCII code for each character? Well, there are a lot of coding in computer programming that require the knowledge of ASCII code. Here we present a simple example that shows the usage of ASCII code in a C++ program.

    Example

    Write a program that provide a user a recommendation of five possible passwords that meet following requirements:

    1. Exactly four characters
    2. The first character must be a digit (0 - 9)
    3. The second character must be a lower case letter
    4. The third character must be an upper case letter
    5. The last character must be a lower case letter
    6. Five passwords have to be random generated instead of being hard coded into your program

    Write a program that provide a user a recommendation of five possible passwords that meet following requirements:

    1. Exactly four characters
    2. The first character must be a digit (0 - 9)
    3. The second character must be a lower case letter
    4. The third character must be an upper case letter
    5. The last character must be a lower case letter
    6. Five passwords have to be random generated instead of being hard coded into your program

    Before we begin the coding process, let's review random function and modulus division.

    In C++, the function rand() produces a random number between 0 and 32,767. If we want to get a random number between 0 and 9, uses rand()%10 because modulus division '%' returns the remainder. Any number is divided by 10, the remainder can only be one of the numbers between 0 and 9. For example, 12 is divided by 10, the remainder is 2; 15 is divided by 10, the remainder is 5; 19 is divided by 10, the remainder is 9; 20 is divided by 10, the remainder is 0; 21 is divided by 10, the remainder is 1, 22 is divided by 10, the remainder is 2 and so on. You can see, as long as the divisor is 10 the remainder can not be anything else except one of the numbers between 0 and 9.

    Similarly, if we want to get a random number between 0 and 99, uses rand()%100.

    Particularly, if we would like to have a random number between 0 and 25, we use rand()%26.

    How about getting a lower case letter randomly? From above table, a lower case letter is represented by an integer between 97 and 122 with 97 and 122 included. To get a random lower case letter,

    Step 1: declare character variable:

    char a;

    Step 2: use rand()%26 to get a random number between 0 and 25

    Step 3: get a random number between 97 and 122 by using 97+rand()%26. According above table, this number can be used as an ASCII code to represent a lower case letter.

    Step 4: store the number we got in Step 3 in the variable we declared in Step 1

    We can get Step 2, 3, and 4 done with a single line of the code shown as following:

    a=97+rand()%26;

    We can display this random lower case letter by following cout statement:

    cout<<a<<endl;

    Similarly, we can get a random upper case letter and display it by using following statements:

    char b;

    b=65+rand()%26;

    cout<<b<<endl;

    Following is a screenshot of the completed program:

    Image 4-1-1.png

    In C++ programming, there are some special characters called Escape Sequences. Following table shows some of those special characters, their notations, and the ASCII codes:

    Escape Sequences

    What is an escape sequence? In C++ programming, most of characters, such as digits, letters, and some of the special character, such as $, &, #, can be embedded in a string of characters. For example, we can using following statements in a C++ program with no issues:

    cout<<"The cost of the textbook is $250"<<endl;
    cout<<"###Schedule###"<<endl;

    However, not all the characters can be embedded in a string as those mentioned above. For example, the character '\' (backlash) can't be directly embedded in a string as we did with '#':

    The following statement will cause an error in your program:

    cout<<"\\\Schedule\\\"<<endl;

    In order to embed a backslash in a string, we need to use \\ (put a backlash in front of it) instead of putting a backslash directly in the string. Thus, if we want to put three backslashes in front of "Schedule" and three backslashes after, we need to code above cout statement as following:

    cout<<"\\\\\\Schedule\\\\\\"<<endl;

    If you are not getting it, let's look another example.

    Suppose we would like to use a cout statement to display following message:

    cout<<"The word "Textbook" means a book that is used for teaching a class"<<endl;

    we will see an error message and the program can't be executed. The reason is the double quote " is one of the character that can't be directly embedded in a string. To correctly code above statement (to put a double quote in a string) we need to put a backslash in front of double quote as shown bellow:

    cout<<"The word \"Textbook\" means a book that is used for teaching a class"<<endl;

    In above two examples, \\ is called an escape sequence of \, and \" is called the an escape sequence of ". To be more specific, if we want to put a backslash or a double quote in a string which we would like to display on the output screen with a cout statement, we can't put them in the string directly, we need to put their escape sequences in the string instead.

    Some of the characters we use in C++ programming don't even have keys on the keyboard to allow us to put them into a string. For example, a linefeed character, which is used for indicating the end of line and the beginning of the next line. In C++, we use \n for such character. Following statement

    cout<<"Fresno City College\nClovis Community College\nReedley College"<<endl;

    will display

    Fresno City College
    Clovis Community College
    Reedley College

    and

    cout<<"Fresno City College Clovis Community College Reedley College"<<endl;

    will display

    Fresno City College Clovis Community College Reedley College

    Commonly Used Escape Sequences in C++

    Escape Sequence Name ASCII Code
    \b Backspace 8
    \t Tab 9
    \n Linefeed 10
    \r Carriage Return 13
    \\ Backslash 92
    \" Double Quote 34

    Casting between char and int

    If we assign a character into an integer variable, the integer variable will receive the ASC II code of the character, for example

    int a = 'A';
    cout<< a <<endl;

    above statements will display the ASCII code of letter A, which is 65, on the screen.

    On the other hand, if we assign an integer into a character variable, the character variable will receive a character with the integer as its ASCII code, for example

    char a = 90;
    cout << a <<endl;

    above statement will display Z on the screen.

    Also, we can use following statement to see ASCII code of a given character, say, &

    cout << static_cast<int>('&') <<endl;

    or we can use following statement to see a character for a given ASCII code, say, 98

    cout << static_cast<char>(98) << endl;

    above static_cast statement will convert a variable or a data from one data type to another data type.

    For example, following statement will convert an integer data 15 from int to double:

    static_cast<double>(15)


    Character Functions

    There is a list of functions that are working on characters, and we call them character functions.

    Function Description
    isdigit(ch) Returns true if the specified character is a digit
    isalpha(ch) Return true if the specified character is a letter
    isalnum(ch) Return true if the specified character is letter or a digit
    islower(ch) Return true if the specified character is a lower case letter
    isupper(ch) Return true if the specified character is an upper case letter
    isspace(ch) Return true if the specified character is a white-space character
    tolower(ch) Return the lowercase of the specified character
    toupper(ch) Return the uppercase of the specified character

    We may not use those functions very often, but whenever we need them, we will know where to find them.


    This page titled 4.1: Character Data Type and ACII Code was last modified on Wed, 24 Dec 2025 20:37:19 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.