Char type and usage examples in C++

Estimated read time 5 min read

The provided C++ code is a demonstration of how to manipulate and display characters and their ASCII values. It also includes a brief explanation of escape characters in C++.

Code

#include <iostream>
using namespace std;

// Main function
int main() {
    // Declare a character variable
    char character = 'A';
    // Print the character
    cout << "Character: " << character << endl;
    // Assign ASCII value of 'A' to the character
    character = 65;
    // Print the character
    cout << "Character (65 in ASCII): " << character << endl;
    // Assign escape character for single quote to the character
    character = '\'';
    // Print the character
    cout << "Character: " << character << endl;
    // Assign escape character for backslash to the character
    character = '\\';
    // Print the character
    cout << "Character: " << character << endl;
    // Assign hexadecimal value for single quote to the character
    character = '\x27';
    // Print the character
    cout << "Character (hexadecimal \\x27): " << character << endl;
    // Assign octal value for single quote to the character
    character = '\047';
    // Print the character
    cout << "Character (octal \\047): " << character << endl;

    // Char types as int values
    /*
    *You can always assign a char value to an int variable;
    *You can always assign an int value to a char variable,
    *but if the value exceeds 255 (the top-most character code in ASCII),
    *you must expect a loss of value;
    *The value of the char type can be subject to the same operators as the data of type int.
    *The value of the char type is always an unsigned char.
     */
    // Assign 'A' + 32 to the character
    character = 'A' + 32;
    // Print the character
    cout << "Character: " << character << endl;
    // Assign 'A' + ' ' to the character
    character = 'A' + ' ';
    // Print the character
    cout << "Character: " << character << endl;
    // Assign 65 + ' ' to the character
    character = 65 + ' ';
    // Print the character
    cout << "Character: " << character << endl;
    // Assign 97 - ' ' to the character
    character = 97 - ' ';
    // Print the character
    cout << "Character: " << character << endl;
    // Assign 'a' - 32 to the character
    character = 'a' - 32;
    // Print the character
    cout << "Character: " << character << endl;
    // Assign 'a' - ' ' to the character
    character = 'a' - ' ';
    // Print the character
    cout << "Character: " << character << endl;

    // Return 0 to indicate successful execution
    return 0;
}

Explanation

The provided C++ code is a demonstration of how to manipulate and display characters and their ASCII values. It also includes a brief explanation of escape characters in C++.

The main function begins by declaring a character variable char character = 'A';. This character is then printed to the console using cout << "Character: " << character << endl;.

The ASCII value of ‘A’, which is 65, is then assigned to the character variable character = 65;. This is again printed to the console, demonstrating that the character ‘A’ and the integer 65 are interchangeable when dealing with char variables.

The code then explores the use of escape characters. Escape characters are special characters that you can include in your text strings such as newline (\n), tab (\t), backspace (\b), etc. In this code, the escape characters for a single quote (\') and a backslash (\\) are assigned to the character variable and printed.

The code also demonstrates how to assign hexadecimal and octal values to the character variable using escape sequences. For example, the hexadecimal value for a single quote is assigned using character = '\x27'; and the octal value is assigned using character = '\047';.

The code then demonstrates some arithmetic operations with characters. For example, it assigns the result of ‘A’ + 32 to the character variable character = 'A' + 32;. This is equivalent to assigning the ASCII value of ‘a’ to the character variable because ‘A’ has an ASCII value of 65 and ‘a’ has an ASCII value of 97, and the difference between these two values is 32.

Finally, the code includes a comment section that provides additional information about char types, their int values, and the use of escape characters in C++.

Output

Character: A
Character (65 in ASCII): A
Character: '
Character: \
Character (hexadecimal \x27): '
Character (octal \047): '
Character: a
Character: a
Character: a
Character: A
Character: A
Character: A

Process finished with exit code 0

Escape Characters

// Explanation of escape characters in C++
// All escape characters can be used in C++ strings to print special characters
// \n = new line character to print new line character in string output
// \t = tab character to print tab character in string output
// \b = backspace character to print backspace character in string output
// \r = carriage return character to print carriage return character in string output
// \f = form feed character to print form feed character in string output
// \v = vertical tab character to print vertical tab character in string output
// \a = alert character to print alert character in string output
// \e = escape character to print escape character in string output
// \0 = null character to print null character in string output
// \\ = backslash character to print backslash character in string output
// \" = double quote character to print double quote character in string output
// \' = single quote character to print single quote character in string output
// \? = question mark character to print question mark character in string output
İbrahim Korucuoğlu

Yazar, bilişim ve teknoloji alanında derlediği faydalı içerikleri bu blogta paylaşmaktadır.