Declaring and displaying char sort variables in C++
|
#embrace <iostream> utilizing namespace std;
int major() {
char ch1 = ‘a’; char ch2 = ‘Z’; char ch3 = ‘7’;
cout <<“First char variable worth = “ <<ch1 <<“n”; cout <<“Second char variable worth = “ <<ch2 <<“n”; cout <<“Third char variable worth = “ <<ch3 <<“n”;
return 0;
} |
Output:
C++ char knowledge sort briefly
- The char is a knowledge sort in C++
- It’s used to retailer a single character
- The character should be enclosed in a single quote e.g. ‘a’, ‘b’, ‘5’
- The char sort takes one byte (8 bits) within the reminiscence
- The worth of char is saved as an integer within the reminiscence
- You might also assign ASCII values to the char sort (See instance within the part beneath)
- Although it will depend on the compiler, the utmost worth {that a} char sort can retailer is often 255.
Assigning ASCII code worth to char sort
Within the following instance, we assigned ASCII values to the three char-type variables. Then we displayed their values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#embrace <iostream> utilizing namespace std;
int major() {
//Declaring char variables with ASCII values char ch1 = 65; char ch2 = 99; char ch3 = 105; char ch4 = 37;
//Show values cout <<“ASCII 65 = “ <<ch1 <<“n”; cout <<“ASCII 99 = “ <<ch2 <<“n”; cout <<“ASCII 105 = “ <<ch3 <<“n”; cout <<“ASCII 37 = “ <<ch4 <<“n”;
return 0;
} |
Output:
You noticed as we assign a price with no single quote, it’s taken as an ASCII worth. As we displayed the variable, it show the corresponding worth to the ASCII character.
Checking reminiscence measurement of char sort
By utilizing sizeof() perform, you might test the scale of a variable. The C++ program beneath shows the scale of char sort variables:
|
#embrace <iostream> utilizing namespace std;
int major() { char ch1 = ‘F’;
cout << “Dimension of char variable: “ << sizeof(ch1) << endl; cout << “Worth of char variable: “ << ch1 << endl;
return 0;
} |
Output:
You noticed the scale is displayed as 1. Which means, one byte of reminiscence taken by the char sort variable
Getting the best worth a char sort can retailer
The instance beneath exhibits the utmost worth a char sort can retailer:
|
#embrace <limits> #embrace <iostream>
int major(int argc, char* argv[]) { std::cout << “Max char restrict: “
<< static_cast<int>(std::numeric_limits<char>::max()) << “n”;
std::cout << “Max unsigned char: “
<< static_cast<int>(std::numeric_limits<unsigned char>::max()) << “n”;
} |
Output:
Get the ASCII worth of Consumer entered character
Within the following program, the person is requested to enter a personality. We’ll then show the ASCII worth of that character:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#embrace <iostream> utilizing namespace std;
int major() { char ch;
//Consumer enter to test ASCII code for
cout << “Enter a Character to Get ASCII code: “;
//Assigning entered worth to char sort cin >> ch;
//Utilizing int perform to get the ASCII code of char sort cout <<“The ASCII code of “<< ch << ” is “<< int(ch) << ‘n’;
return 0;
} |
Pattern Output 1:
Pattern Output 1:
Pattern Output 1: