Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Char | Text Data Type
C++ Data Types

Char

To store text data in C++, the char data type is used to store a single character, like 'A' or 'w'. In the upcoming chapter, we will explore the process of combining these individual characters into sequences to form words, sentences, and more. However, for now, let's focus on the char data type for single character storage.

cpp

main.cpp

Note

char's should be specified in single quotes. Even if the character you hold is a number, you should put it in single quotes, '9', not 9.

You can play with the code above to see what happens if you use double quotes or assign numbers without quotes.

The char data type and memory.

To be stored in memory, it is first converted to a number using ASCII table. The binary representation of that number is then stored in memory.
You can take a quick look at the ASCII table below (the first column is not valuable for us).

ASCII Table
Decimal Character Decimal Character Decimal Character Decimal Character
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH 33 ! 65 A 97 a
2 STX 34 66 B 98 b
3 ETX (End of text) 35 # 67 C 99 c
4 EOT 36 $ 68 D 100 d
5 ENQ 37 % 69 E 101 e
6 ACK 38 & 70 F 102 f
7 BEL 39 71 G 103 g
8 BS 40 ( 72 H 104 h
9 HT (Horizontal tab) 41 ) 73 I 105 i
10 LF (New line) 42 * 74 J 106 j
11 VT (Vertical tab) 43 + 75 K 107 k
12 FF 44 , 76 L 108 l
13 CR 45 77 M 109 m
14 SO 46 . 78 N 110 n
15 SI 47 / 79 O 111 o
16 DLE 48 0 80 P 112 p
17 DC1 49 1 81 Q 113 q
18 DC2 50 2 82 R 114 r
19 DC3 51 3 83 S 115 s
20 DC4 52 4 84 T 116 t
21 NAK 53 5 85 U 117 u
22 SYN 54 6 86 V 118 v
23 ETB 55 7 87 W 119 w
24 CAN 56 8 88 X 120 x
25 EM 57 9 89 Y 121 y
26 SUB 58 : 90 Z 122 z
27 ESC) 59 ; 91 [ 123 {
28 FS 60 < 92 \ 124 |
29 GS 61 = 93 ] 125 }
30 RS 62 > 94 ^ 126 ~
31 US 63 ? 95 _ 127 DEL
cpp

main.cpp

Note

If you assign a number without single quotes to a char (for example, char letter = 76), the compiler assumes that you specified a character already converted to a number.

As you can see from the table, 76 corresponds to L, so the value of letter is 'L'.

Everything was clear?

Section 3. Chapter 1
course content

Course Content

C++ Data Types

Char

To store text data in C++, the char data type is used to store a single character, like 'A' or 'w'. In the upcoming chapter, we will explore the process of combining these individual characters into sequences to form words, sentences, and more. However, for now, let's focus on the char data type for single character storage.

cpp

main.cpp

Note

char's should be specified in single quotes. Even if the character you hold is a number, you should put it in single quotes, '9', not 9.

You can play with the code above to see what happens if you use double quotes or assign numbers without quotes.

The char data type and memory.

To be stored in memory, it is first converted to a number using ASCII table. The binary representation of that number is then stored in memory.
You can take a quick look at the ASCII table below (the first column is not valuable for us).

ASCII Table
Decimal Character Decimal Character Decimal Character Decimal Character
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH 33 ! 65 A 97 a
2 STX 34 66 B 98 b
3 ETX (End of text) 35 # 67 C 99 c
4 EOT 36 $ 68 D 100 d
5 ENQ 37 % 69 E 101 e
6 ACK 38 & 70 F 102 f
7 BEL 39 71 G 103 g
8 BS 40 ( 72 H 104 h
9 HT (Horizontal tab) 41 ) 73 I 105 i
10 LF (New line) 42 * 74 J 106 j
11 VT (Vertical tab) 43 + 75 K 107 k
12 FF 44 , 76 L 108 l
13 CR 45 77 M 109 m
14 SO 46 . 78 N 110 n
15 SI 47 / 79 O 111 o
16 DLE 48 0 80 P 112 p
17 DC1 49 1 81 Q 113 q
18 DC2 50 2 82 R 114 r
19 DC3 51 3 83 S 115 s
20 DC4 52 4 84 T 116 t
21 NAK 53 5 85 U 117 u
22 SYN 54 6 86 V 118 v
23 ETB 55 7 87 W 119 w
24 CAN 56 8 88 X 120 x
25 EM 57 9 89 Y 121 y
26 SUB 58 : 90 Z 122 z
27 ESC) 59 ; 91 [ 123 {
28 FS 60 < 92 \ 124 |
29 GS 61 = 93 ] 125 }
30 RS 62 > 94 ^ 126 ~
31 US 63 ? 95 _ 127 DEL
cpp

main.cpp

Note

If you assign a number without single quotes to a char (for example, char letter = 76), the compiler assumes that you specified a character already converted to a number.

As you can see from the table, 76 corresponds to L, so the value of letter is 'L'.

Everything was clear?

Section 3. Chapter 1
some-alt