Lab 2: Experiment 4


The Program

For this experiment, you will write code for character variables.

The keyword for the character data type in C++ is char. It represents exactly one character.

A character literal begins and ends with single quotes, with the character between them (e.g., 'a' and 'A').

Character Declarations

Write declarations for char variables ch1 and ch2, initializing them to 'a' and 'Z', respectively. Do not get rid of any code. Compile and execute your program.

Question #2.4.1: What is the declaration that you added to your program?

You won't see any change in the execution since the declaration and initialization is purely internal. Once you can compile and execute the code without problems, add a statement to print out ch1 and ch2. The original program had this statement to print i and j:

cout << "i is " << i << "\n"
     << "j is " << j << endl;
Your new statement for ch1 and ch2 should look quite similar, replacing the integer variables with the character variables and changing the labels appropriately.

Now compile and execute the program. Make sure it prints the proper labels and values for the variables.

Question #2.4.2: What is the ouput statement you just added to your program?

Let's play around with these declarations.

Character Initialization

In the integer experiment, we tried initializing an integer variable with various different types. Let's try this again for the char variables you just added to your program. Again, "I didn't get any compiler errors or warnings" is an acceptible answer for these questions.

Initialize ch1 to an integer (try something between 64 and 100). Compile your program.

Question #2.4.3: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it print for the new initialization?

Initialize ch1 to a double. Compile your program.

Question #2.4.4: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it print for the new initialization?

Initialize ch1 to a string. Compile your program.

Question #2.4.5: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it print for the new initialization?

Initialize ch1 to be equal to cin. Compile your program.

Question #2.4.6: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it print for the new initialization?

Change your program back so that ch1 is initialized to a character.

Character Literals

As mentioned above, a character literal consists of two single quotes with a character between them.

What happens if there's more than one character between them? Change the character literal for ch1 to be 'abc'. Compile your program.

Question #2.4.7: What is the first error or warning message that the compiler gives you? If it does compile (with or without warnings), what does it print for the new initialization?

There are some characters that perform an action. For example, if you want the displayed output to start on a new line you must use the newline character. But you're using the newline character in your program to control how it looks, so it's not entirely clear how you would enter a newline into a string so that it appears in the program's output.

C++ solves this mild dilemma with escape characters. An escape character begins with the backslash character, \, followed by another character that represents the character we want. For example, the newline character literal is '\n'. You, the programmer, must type in two separate characters, but to C++ this represents a single character, the newline character.

Some other escape characters include '\t' for the tab character, '\'' for a single quote, '\"' for a double quote (more useful in strings), and '\\' for a backslash character.

Initialize ch1 to be the newline character and ch2 to be a single quote.

Question #2.4.8: Predict: what will your program display when you execute it?

Compile and execute your program to test your prediction.

Question #2.4.9: What did your program actually display? Compare the actual output to your prediction.

As you'll see in the next experiment, these escape characters are also used in strings.

Terminology

escape character
Back to the Lab Exercise  |  Forward to the Next Experiment
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.