Feiyu BLOG 2023.3.31-
Information Technology Education
Python-Based Teaching
Research Topics
Academic Level Examination
Python Program Design
C++ Informatics
……


Variables are like a warehouse, representing a storage unit, and their values can change during the execution of a program.
In C++, the format for defining a variable is as follows:
Format: data type variable_name1, variable_name2, ……, variable_n
Function: Allocates n spaces of the specified data type in computer memory, named variable_name1, variable_name2, ……, variable_n.
For example: int a, which allocates a space of integer data type with the variable name a in computer memory.
Common variable types include integer int, floating-point float, and character char, etc.
Exercise Program:
#include <iostream>
using namespace std;
int main(){
int a,b;
a=2;
b=5;
cout<<"a="<<a<<" "
<<"b="<<b<<endl;
//endl means new line
b=8;
cout<<"a="<<a<<" "
<<"b="<<b<<endl;
}
Debugging Results:
a=2 b=5
a=2 b=8
After a variable is defined, the system allocates a storage space for it in the computer. When a variable is used in the program, the system will store or retrieve data in the corresponding memory. This operation is called variable access.
Variable Naming Rules:
A program may need to use multiple variables, and to distinguish between them, each variable must be given a name, known as the variable name. The naming rules for variables in C++ are as follows:
(1) Variable names may only contain letters, numbers, or underscores;
(2) The first character of a variable name cannot be a number and cannot contain other characters;
(3) Variable names cannot be C++ keywords, such as main, etc.
(4) Variables must be “defined before use”; variable names are case-sensitive. It is recommended that variable names do not exceed 8 characters in length.
Exercises:
1. Read the program and write the final running result on the line below.
#include <iostream>
using namespace std;
int main(){
int a;
a=10;
a=20;
a=30;
a=40;
a=50;
cout<<a<<endl;
}
Running Result:________________
2. Store the real number 5.6 in the computer memory variable b and output it.
1.IC002 – C++ Writing Hello World2.CSP-J/S Examination3.Informatics Competition 001 – Dev C++ Installation
Improve by1%, and over a year you can achieve37 times growth; if you regress by1% daily, you will weaken, even regress to zero; a small change or a good habit can create an amazing compounding effect!