Saturday 18 February 2012

The char Data Type in C++

Here is a brief discussion on the char data type in C++.

The sample code was created in MS Visual C++ as a Windows Form Application. It contains a form (Form1) that displays when the program runs. The sample code, contained in the Form Load Event, executes and adds text to a RichTextBox named rtOut.

1. Declaring A Character Variable

The most common way to declare a char variable is:

char cVarName;

char is a C++ keyword used to declare the user-named variable cVarName.

You get the same result by using the signed modifier in the declaration. A modifier is also referred to as a specifier.

signed char cVarName;

In both cases, cVarName is a 1 byte character variable. The range of its value is -128 to 127 for a total of 256 possible values.

You can assign an integer value to the variable when you declare it:

char cVarName = 65;

Or you can assign a single character to the variable.

char cVarName = 'A';

Note the use of the single quotation marks instead of double quotation marks. Single quotes are used for a character. Double quotes are used for strings. If you used double quotes, the compiler will note the error and not finish.

In both cases, the program will store the ASCII code 65 in memory for cVarName.

The third way to use the char declaration is with the unsigned modifier.

unsigned char cVarName;

In this instance, cVarName holds only positive values from 0 to 255. 1 byte (8 bits) allows for 256 possible values.

2. Storing Data In A Character Variable.

You can assign an initial value in your declaration.

char cVarName = 'A';
char cVarName = 65;

The following examples however will result in a compile error.

cVarName = "A";

The use of double quotes is for strings and this variable is a character, not a string.

cVarName = '';

The empty character is not a character nor is it null or zero. The assignment of an empty character will result in a compile error.

cVarName = void;

Similar to the empty character. You cannot assign void to a character variable.

The following assignments will work.

// Space or ASCII code 32.
cVarName = ' ';
cVarName = 32;

// Null.
cVarName = '\0';
cVarName = 0;


3. Char Size & Range

In all instances, the character variable is 1 byte in size. Since 1 byte is 8 bits, we know there are 256 possible variations. 2 to the power of 8 is 256. Since 0 is included, the range is 0 to 255 for unsigned and -128 to 127 for signed.

The compiler will not object to assigning values outside the range, nor will the program crash. For example,

cVarName = 123654987;

will work; however, the value you retrieve from the variable will be some number in the range for the data type and not the out-of-range value.


4. Sample Code: Signed char

// [Signed] Character type.
// 1 byte.
// Range: -128 to 127

// Start.
rtOut->Text = "Character type: [signed] char variable_name";
// Line feed.
rtOut->Text += "\n";

rtOut->Text += "1 byte, Range: -128 to 127";
rtOut->Text += "\n";
rtOut->Text += "\n";

// Declare signed char variable.
signed char cB = ' ';

cB = -129;
rtOut->Text += "Set variable to -129 (cB = -129;) results in " + cB + ". It's out of range. \n";

cB = -128;
rtOut->Text += "Set variable to -128 (cB = -128;) results in " + cB + ". It's in range. \n";

cB = 127;
rtOut->Text += "Set variable to 127 (cB = 127;) results in " + cB + ". It's in range. \n";

cB = 128;
rtOut->Text += "Set variable to 128 (cB = 128;) results in " + cB + ". It's out of range. \n";

Output:

Character type: [signed] char variable_name
1 byte, Range: -128 to 127

Set variable to -129 (cB = -129;) results in 127. It's out of range.
Set variable to -128 (cB = -128;) results in -128. It's in range.
Set variable to 127 (cB = 127;) results in 127. It's in range.
Set variable to 128 (cB = 128;) results in -128. It's out of range.


5. Sample Code: Unsigned char

// Unsigned character type.
// 1 byte.
// Range: 0 to 255

unsigned char cC;

// Start.
rtOut->Text += "Character type: unsigned char variable_name";
rtOut->Text += "\n";
rtOut->Text += "1 byte, Range: 0 to 255";
rtOut->Text += "\n";
rtOut->Text += "\n";

cC = 0;
rtOut->Text += "Set variable to 0 (cC = 0;) results in " + cC + ". It's in range.";
rtOut->Text += "\n";

cC = 127;
rtOut->Text += "Set variable to 127 (cC = 127;) results in " + cC + ". It's in range.";
rtOut->Text += "\n";

cC = 256;
rtOut->Text += "Set variable to 256 (cC = 256;) results in " + cC + ". It's out of range.";
rtOut->Text += "\n";

cC = -1;
rtOut->Text += "Set variable to -1 (cC = -1;) results in " + cC + ". It's out of range.";
rtOut->Text += "\n";

Output:

Character type: unsigned char variable_name
1 byte, Range: 0 to 255

Set variable to 0 (cC = 0;) results in 0. It's in range.
Set variable to 127 (cC = 127;) results in 127. It's in range.
Set variable to 256 (cC = 256;) results in 0. It's out of range.
Set variable to -1 (cC = -1;) results in 255. It's out of range.

No comments:

Post a Comment