Data Storage — Variables
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 int sideLength;
14 sideLength = 40;
15
16 turtle.forward(sideLength);
17 turtle.right(22.5);
18 turtle.forward(sideLength);
19 turtle.right(22.5);
20 turtle.forward(sideLength);
21 turtle.right(22.5);
22 turtle.forward(sideLength);
23 turtle.right(22.5);
24 turtle.forward(sideLength);
25 turtle.right(22.5);
26 turtle.forward(sideLength);
27 turtle.right(22.5);
28 turtle.forward(sideLength);
29 turtle.right(22.5);
30 turtle.forward(sideLength);
31 turtle.right(22.5);
32 turtle.forward(sideLength);
33 turtle.right(22.5);
34 turtle.forward(sideLength);
35 turtle.right(22.5);
36 turtle.forward(sideLength);
37 turtle.right(22.5);
38 turtle.forward(sideLength);
39 turtle.right(22.5);
40 turtle.forward(sideLength);
41 turtle.right(22.5);
42 turtle.forward(sideLength);
43 turtle.right(22.5);
44 turtle.forward(sideLength);
45 turtle.right(22.5);
46 turtle.forward(sideLength);
47 turtle.right(22.5);
48
49 // Pause the program
50 system("pause");
51 // End of program
52 return 0;
53}1#include "CTurtle.hpp"
2#include <bits/stdc++.h>
3using namespace std;
4using namespace cturtle;
5
6int main() {
7 // Create a canvas screen
8 TurtleScreen scr;
9 // Create a drawing turtle
10 Turtle turtle(scr);
11
12 int sideLength = 40;
13 double angle = 22.5;
14
15 turtle.forward(sideLength);
16 turtle.right(angle);
17 turtle.forward(sideLength);
18 turtle.right(angle);
19 turtle.forward(sideLength);
20 turtle.right(angle);
21 turtle.forward(sideLength);
22 turtle.right(angle);
23 turtle.forward(sideLength);
24 turtle.right(angle);
25 turtle.forward(sideLength);
26 turtle.right(angle);
27 turtle.forward(sideLength);
28 turtle.right(angle);
29 turtle.forward(sideLength);
30 turtle.right(angle);
31 turtle.forward(sideLength);
32 turtle.right(angle);
33 turtle.forward(sideLength);
34 turtle.right(angle);
35 turtle.forward(sideLength);
36 turtle.right(angle);
37 turtle.forward(sideLength);
38 turtle.right(angle);
39 turtle.forward(sideLength);
40 turtle.right(angle);
41 turtle.forward(sideLength);
42 turtle.right(angle);
43 turtle.forward(sideLength);
44 turtle.right(angle);
45 turtle.forward(sideLength);
46 turtle.right(angle);
47
48 // Pause the program
49 system("pause");
50 // End of program
51 return 0;
52}1
2#include <iostream>
3#include <string>
4using namespace std;
5
6int main() {
7 // Define variables of different types
8 int number; //Create an int variable
9 double dnumber; // Create a double variable
10 float fnumber; // Create a float variable
11 char ch; // Create a char variable
12
13 cin >> number >> dnumber >> fnumber >> ch; // Read data into variables
14 cout << fixed << setprecision(2) << number << endl; //Set output precision
15 cout << dnumber << endl; // Output decimal
16 cout << fnumber << endl; // Output decimal
17 cout << ch << endl; //Output character
18
19 return 0; // Program ends normally
20}1//
2#include <iostream>
3//scanf and printf don't need the std namespace
4
5int main() {
6 int number; //Create an int variable
7 scanf("%d", &number); // Read an integer into number
8 printf("%d\n", number); // Output integer
9
10 double dnumber; // Create a double variable
11 scanf("%lf", &dnumber); // Read a decimal into dnumber
12 printf("%lf\n", dnumber); // Output decimal using .lf
13
14 float fnumber; // Create a float variable
15 scanf("%f", &fnumber); // Read a decimal into fnumber
16 printf("%.3f\n", fnumber); // Output with 3 decimal places
17
18 char ch; // Create a char variable
19 scanf(" %c", &ch); // Read a space and a character into ch
20 printf("%c\n", ch); // Output character
21
22 return 0; // Program ends normally
23}1//
2#include <bits/stdc++.h>
3using namespace std;
4int main() {
5 int a, b;
6 cin >> a >> b;
7 cout << a + b << endl;
8
9
10 return 0;
11}1//
2#include <bits/stdc++.h>
3using namespace std;
4
5int main() {
6 char ch;
7 cin >> ch;
8 cout << int(ch) << endl;
9
10 return 0;
11}1//
2#include <bits/stdc++.h>
3using namespace std;
4
5int main() {
6 float number;
7 cin >> number;
8 printf("%.3f", number);
9
10 return 0;
11}II. Knowledge Summary
✨ Defining and Using Variables
A variable is a container used to store data in a program. Each variable has its own name and data type.
When defining a variable, you first specify the data type, then the variable name. Definition and assignment can be separate, or you can assign a value at definition time (called initialization):
int length; //Define first: create an int variable
length = 200; //Assign later: assign 200 to length
double angle = 360.0 / 4; //Define and initialize: create variable and assign 90.0When using a variable, simply write the variable name. Do not add the data type before the variable name again, or it will be treated as redefining a new variable:
cout << length << endl; //Correct: directly use the variable name
cout << angle << endl; //Correct: directly use the variable name
int length = 100; //Error: redefined the length variable✨ Defining and Using Constants
A constant is a special type of variable whose value cannot be changed after definition. It is declared using the const keyword and must be assigned a value at definition time.
Constants are typically used to store fixed values that won't change in the program, such as mathematical constants, array sizes, etc. They improve code readability and safety (preventing accidental modification).
const int MAX_SIZE = 100; //Define an integer constant
const double PI = 3.14159; //Define a decimal constant
cout << MAX_SIZE << endl; //Correct: can use the constant normally
cout << PI * 2 << endl; //Correct: constants can be used in calculations
MAX_SIZE = 200; //Error! Constants cannot be modified, compilation error✨ Naming Rules for Variables and Constants
When naming variables and constants, you must follow C++ naming conventions. Good naming habits make code more readable.
Mandatory rules:
- Can only use English letters, underscores, and digits
- Cannot start with a digit (e.g.,
2numis invalid,num2is valid) - Cannot use C++ keywords (such as
int,return, etc.) - Variable names are case-sensitive:
int a;andint A;are two different variables
Recommended naming conventions:
- Regular variables: Start with lowercase letters, use camelCase (
studentName) or snake_case (student_name) for multiple words - Constants: Use ALL UPPERCASE, separate words with underscores, e.g.,
MAX_SIZE,PI - Names starting with underscore generally have special meanings and should be avoided
✨ C++ Keywords List
The following are reserved keywords in C++. These words have special meanings and cannot be used as variable or constant names:
| auto | FALSE | static | using | if |
| goto | static_cast | bool | TRUE | public |
| namespace | else | and | const_cast | char |
| enum | protected | inline | for | not |
| dynamic_cast | short | struct | virtual | delete |
| do | xor | static_assert | long | class |
| override | this | switch | return | register |
| float | wchar_t | final | nullptr | case |
| try | explicit | double | sizeof | operator |
| void | default | extern | signed | typeid |
| const | friend | break | throw | unsigned |
| typedef | constexpr | template | continue | noexcept |
✨ C++ Style Input
C++ uses cin with the extraction operator >> to read input, and cout with the insertion operator << for output. The advantage of C++ style is that it doesn't require specifying data types — the compiler automatically matches based on variable types, making it simpler to use.
1#include <iostream>
2#include <string>
3using namespace std;
4
5int main() {
6 // Define variables of different types
7 int number; //Create an int variable
8 double dnumber; // Create a double variable
9 float fnumber; // Create a float variable
10 char ch; // Create a char variable
11
12 cin >> number >> dnumber >> fnumber >> ch; // Read data into variables
13 cout << fixed << setprecision(2) << number << endl; //Set output precision
14 cout << dnumber << endl; // Output decimal
15 cout << fnumber << endl; // Output decimal
16 cout << ch << endl; //Output character
17
18 return 0; // Program ends normally
19}✨ C Style Input
C language uses the scanf function for input and printf for output. Unlike C++ style, C style requires specifying data types through format specifiers. The syntax is slightly more complex but is more convenient when controlling output format (such as decimal places).
Common format specifiers:
| Format Specifier | Data Type | Description |
|---|---|---|
%d | int | Read/output integer |
%lld | long long | Read/output long integer |
%f | float | Read/output single-precision float |
%lf | double | Read double (scanf uses %lf; printf can use %f or %lf) |
%c | char | Read/output single character |
%s | char[] | Read/output string |
1#include <iostream>
2//scanf and printf don't need the std namespace
3
4int main() {
5 int number; //Create an int variable
6 scanf("%d", &number); // Read an integer into number
7 printf("%d\n", number); // Output integer
8
9 double dnumber; // Create a double variable
10 scanf("%lf", &dnumber); // Read a decimal into dnumber
11 printf("%lf\n", dnumber); // Output decimal using .lf
12
13 float fnumber; // Create a float variable
14 scanf("%f", &fnumber); // Read a decimal into fnumber
15 printf("%.3f\n", fnumber); // Output with 3 decimal places
16
17 char ch; // Create a char variable
18 scanf(" %c", &ch); // Read a space and a character into ch
19 printf("%c\n", ch); // Output character
20
21 return 0; // Program ends normally
22}III. Homework
Programming Exercises
- Integer and Boolean Conversion: L1064
- Print Character: L1065
- Output the Second Integer: L1066
- Division with Remainder: L1067