C++ Basic Data Types
I. In-Class Exercises
Programming Exercises
Sample Code
1
2#include "CTurtle.hpp"
3#include <bits/stdc++.h>
4using namespace std;
5using namespace cturtle;
6
7int main() {
8 // Create a canvas screen
9 TurtleScreen scr;
10 // Create a drawing turtle
11 Turtle turtle(scr);
12
13 turtle.right(50);
14 turtle.forward(22.5);
15 turtle.right(50);
16 turtle.forward(22.5);
17 turtle.right(50);
18 turtle.forward(22.5);
19 turtle.right(50);
20 turtle.forward(22.5);
21 turtle.right(50);
22 turtle.forward(22.5);
23 turtle.right(50);
24 turtle.forward(22.5);
25 turtle.right(50);
26 turtle.forward(22.5);
27 turtle.right(50);
28 turtle.forward(22.5);
29 turtle.right(50);
30 turtle.forward(22.5);
31 turtle.right(50);
32 turtle.forward(22.5);
33 turtle.right(50);
34 turtle.forward(22.5);
35 turtle.right(50);
36 turtle.forward(22.5);
37 turtle.right(50);
38 turtle.forward(22.5);
39 turtle.right(50);
40 turtle.forward(22.5);
41 turtle.right(50);
42 turtle.forward(22.5);
43 turtle.right(50);
44 turtle.forward(22.5);
45
46 // Pause the program
47 system("pause");
48 // End of program
49 return 0;
50}1//
2#include "CTurtle.hpp"
3#include <bits/stdc++.h>
4using namespace std;
5using namespace cturtle;
6
7int main() {
8 cout << 10 << endl;
9 cout << "10: " << sizeof(10) << endl;
10 cout << sizeof(2147483647) << endl;
11 cout << sizeof(2147483648) << endl;
12 cout << sizeof(int) << endl;
13 cout << sizeof(long long) << endl;
14 cout << 10.4 << endl;
15 cout << sizeof(10.4) << endl;
16 cout << sizeof(float) << endl;
17 cout << sizeof(double) << endl;
18 cout << 10.0 << endl;
19 cout << "10.0: " << sizeof(10.0) << endl;
20 cout << "Hello, world!" << endl;
21 cout << "e" << endl;
22 cout << sizeof("e") << endl;
23 cout << 'e' << endl;
24 cout << sizeof('e') << endl;
25 cout << 1 << endl;
26 cout << 1.0 << endl;
27 cout << '1' << endl;
28 cout << true << endl;
29 cout << false << endl;
30 cout << (bool)3.5 << endl;
31 cout << float(3.5) << endl;
32 cout << static_cast<int>(3.5) << endl;
33 cout << 'a' + 1 << endl;
34 cout << true + 'a' << endl;
35 return 0;
36}1//
2#include <bits/stdc++.h>
3using namespace std;
4
5int main() {
6 cout << "10 + 3 = " << 10 + 3 << endl;
7 cout << "10 * 3 = " << 10 * 3 << endl;
8 cout << "10 / 3 = " << 10 / 3 << endl;
9 return 0;
10}1//
2#include <bits/stdc++.h>
3using namespace std;
4
5int main() {
6 cout << (char)48 << endl;
7 cout << bool(10) << endl;
8 cout << (int)'A' << endl;
9 cout << (int)10.9 << endl;
10 cout << char('z' - 1) << endl;
11 return 0;
12}II. Knowledge Summary
✨ Four Basic Data Types
Integer Types (int/long long, etc.)
Integer types are used to store numbers without decimal parts and are the most commonly used data types in competitive programming. Different integer types occupy different memory sizes and can represent different ranges. Generally, int is sufficient; when data exceeds 2.1 billion (2^31-1), use long long. Adding the unsigned prefix indicates an unsigned integer that can only store non-negative numbers but has double the range.
| Keyword | Memory Size | Data Range |
|---|---|---|
| short | 16 bits / 2 bytes | -2^15 to 2^15 - 1 / -32768 to 32767 |
| int (default) | 32 bits / 4 bytes | -2^31 to 2^31 - 1 / -2147483648 to 2147483647 |
| long | 32 bits / 4 bytes | -2^31 to 2^31 - 1 / -2147483648 to 2147483647 |
| long long | 64 bits / 8 bytes | -2^63 to 2^63 - 1 / -9223372036854775808 to 9223372036854775807 |
| unsigned short | 16 bits / 2 bytes | 0 to 2^16 - 1 / 0 to 65535 |
| unsigned int | 32 bits / 4 bytes | 0 to 2^32 - 1 / 0 to 4294967295 |
| unsigned long | 32 bits / 4 bytes | 0 to 2^32 - 1 / 0 to 4294967295 |
| unsigned long long | 64 bits / 8 bytes | 0 to 2^64 - 1 / 0 to 18446744073709551615 |
Floating-Point Types (float/double)
Floating-point types are used to store numbers with decimal parts. Different floating-point types have different precision: float is accurate to 6-7 decimal places, double to 15-16. In competitive programming, double is generally used because it has higher precision and is the default type for decimals in C++ (e.g., 3.14 defaults to double).
| Keyword | Memory Size | Data Range |
|---|---|---|
| float | 32 bits / 4 bytes | ±3.4×10^38 / 6-7 decimal places |
| double (default) | 64 bits / 8 bytes | ±1.7×10^308 / 15-16 decimal places |
| long double | 128 bits / 16 bytes | ±3.4×10^3932 / 33-34 decimal places |
Character Type (char)
The character type is used to store a single character, such as letters, digit symbols, or special symbols. Characters are actually stored as integers in the computer, where each character corresponds to an ASCII code value. Therefore, characters can participate in arithmetic operations (e.g., 'a' + 1 gives the ASCII value of 'b').
| Keyword | Memory Size | Data Range |
|---|---|---|
| char | 8 bits / 1 byte | 0-127 |
ASCII Table
Common ASCII values:
| ASCII Value | Character | Description |
|---|---|---|
| 0 | \0 | Null character |
| 32 | (space) | Space character |
| 48 | 0 | Digit 0 |
| 57 | 9 | Digit 9 |
| 65 | A | Uppercase A |
| 90 | Z | Uppercase Z |
| 97 | a | Lowercase a |
| 122 | z | Lowercase z |
| 127 | DEL | Delete character |
ASCII Patterns:
- Digits
0-9correspond to 48-57 (consecutive) - Uppercase letters
A-Zcorrespond to 65-90 (consecutive) - Lowercase letters
a-zcorrespond to 97-122 (consecutive) - The difference between uppercase and corresponding lowercase ASCII values is 32 (e.g.,
A=65,a=97)
Boolean Type (bool)
The boolean type is used to represent logical true/false, with only two values: true and false. Boolean values are stored as integers in the computer: true corresponds to 1, false to 0. In C++, any non-zero value is treated as true, while 0 is treated as false.
| Keyword | Memory Size | Data Range |
|---|---|---|
| bool | 8 bits / 1 byte | 0 (false) to 1 (true) |
✨ Type Conversions Between the Four Data Types
| Conversion | From Integer | From Float | From Char | From Bool |
|---|---|---|---|---|
| To Integer | - | Truncates decimal part, e.g., (int)3.5=3 | Converts via ASCII, e.g., (int)'0'=48 | (int)true=1, (int)false=0 |
| To Float | Converts directly, e.g., (double)3=3 | - | Converts via ASCII, e.g., (double)'0'=48 | (double)true=1, (double)false=0 |
| To Char | Converts via ASCII, e.g., (char)48='0' | First converts to int then via ASCII, e.g., (char)48.8='0' | - | (char)true='\1', (char)false='\0' |
| To Bool | Non-zero becomes true, 0 becomes false, e.g., (bool)3=1 | Non-zero becomes true, 0 becomes false, e.g., (bool)3.5=1 | Converts via ASCII, e.g., (bool)'0'=1, (bool)'\0'=0 | - |