A byte
- Is used to hold a single character
A character
- Is an individual symbol that displays on your screen
- It could be (B-1)
Example of (B-1)
- A lowercase letter: a, b, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, and z
- An uppercase letter: A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, And Z
- A digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9
- A special characters: ‘ ~ # $ ! @ % ^ & * ( { [ ) } ] | \ : ; ・・+ – < _ ? > , / =.
To declare a variable as a character
- Use the C++ keyword char followed by a valid C++ name. (A-1)
Example of (A-1)
char Gender;
With a byte variable declared like (A-1)
- You can give the variable a starting value
Giving a value to a variable
- Is referred to as initializing it
- Initializing a variable is done with the = operator
- The syntax used is (A-2):
Example of (A-2)
VariableName = Value;
Since a char variable represents one symbol
- To initialize it, enclose the initial value in single quotes
- Here is an example (A-3):
Example of (A-3)
char Answer = 'y';
You can also initialize a variable
- By including its value between parentheses as (A-4)
Example of (A-4)
char Answer('y');
To display the value of an initialized variable
- Use the cout << extractor and type the name of the variable
- Here is an example (A-5):
Example of (A-5)
#include <iostream> using namespace std; int main() { char category = 'H'; cout << "Category: " << category; cout << endl; return 0; }
If the character is of a signed category
- You can declare it as a signed character
- This type of variable would be an 8-bit integer whose value can range from -128 to +127
- Here is an example (A-6):
Example of (A-6)
#include <iostream> using namespace std; int main() { signed char category = 'D'; cout << "Category: " << category; cout << endl; return 0; }
You can also declare a positive character
- As unsigned char
- Here is an example (A-7):
Example of (A-7)
#include <iostream> using namespace std; int main() { char letter = 'L'; char category = 'n'; unsigned char sound('x'); cout << "Letter " << letter; cout << "Category " << category; cout << "Sound " << sound; return 0; }
To request a byte variable
- Type the name of the variable on the right side of the cin >> extractor followed by a semi-colon
- Here is an example (A-8):
Example of (A-8)
#include <iostream> using namespace std; int main() { char satisfaction, answer; signed char ageCategory, size; cout << "From A to Z, enter a character as your level of satisfaction: "; cin >> satisfaction; cout << "Age category(t=teen/a=Adult/s=Senior): "; cin >> ageCategory; cout << "Are you a nerd(y=Yes/n=No)? "; cin >> answer; cout << "Enter your size(s=Small/m=Medium/l=Large): "; cin >> size; cout << "\nSatisfaction: " << satisfaction; cout << "\nAge category: " << ageCategory; cout << "\nYour answer: " << answer; cout << "\nYour size: " << size; return 0; }
To keep the integer value of a char positive
- You can declare it as an unsigned char
- Its value would then range from 0 to 255

Discussion
No comments for “Character Variables”