Mastering the use of variables in C++ from definition to practice
C++ variable
A variable is a container used to store data values.
In C++, there are different types of variables (defined using different keywords), such as:
int
- Store integers (no decimal points), for example123
或- 123.
double
- Store floating-point numbers with decimal points, for example19.99
或19.99
char
- Stores a single character, for example'a'
或'B'
. Character values are enclosed in single quotation marksstring
- Store text, for example"Hello World"
. String values are enclosed in double quotesbool
- Stores values with two states:true
或false
Declare (create) variables
To create a variable, specify the type and assign a value to it:
grammar
type variableName = value;
Where type is one of the C++ types (such as int) and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to a variable.
To create a variable that should store numbers, refer to the following example:
Give an example
Create a variable named myNum of type int and assign it a value of 15:
int myNum = 15; cout << myNum;
You can also declare a variable without assigning a value and assign a value later:
Give an example
int myNum; myNum = 15; cout << myNum;
Note that if you assign a new value to an existing variable, it will override the previous value:
Give an example
int myNum = 15; // myNum is 15. myNum = 10; // myNum is now 10 cout << myNum; // Output 10
Other types
Demonstrations of other data types:
Give an example
int myNum = 5; // Integer (no decimal) double myFloatNum = 5.99; // floating-point number (with decimal points) char myLetter = 'D'; // string myText = "Hello"; // String (text) bool myBoolean = true; // Boolean value (true or false)
Display variable
The cout object is used with the << operator to display variables.
To combine text and variables, use the << operator to separate them:
Give an example
int myAge = 35; cout << "I am " << myAge << " years old.";
Add variables
To add one variable to another, you can use the + operator:
Give an example
int x = 5; int y = 6; int sum = x + y; cout << sum;
C++ identifier
All C++ variables must be identified by a unique name.
These unique names are called identifiers.
Identifiers can be short names (such as x and y) or more descriptive names (age, sum, totalVolume).
Note: Descriptive names are recommended to create understandable and maintainable code:
Give an example
// OK int minutesPerHour = 60; // Yes, but it's not easy to understand what m is. int m = 60;
The general rule for naming variables is:
- The name can contain letters, numbers, and underscores
- Name must start with a letter or underscore (_)
- Name is case sensitive (myVar and myvar are different variables)
- The name cannot contain Spaces or special characters, such as! , #, %, etc.
- Reserved words (such as C++ keywords, such as int) cannot be used as names
C++ constant
When you don't want someone else (or yourself) to change the value of an existing variable, use the const keyword (this will declare the variable as a "constant", i.e. unchangeable and read-only) :
Give an example
const int myNum = 15; // myNum will always be 15 myNum = 10; // Error: Attempt to assign read-only variable 'myNum'
You should always declare a variable as a constant when you have values that are unlikely to change:
Give an example
const int minutesPerHour = 60; const float PI = 3.14;
A note on constants
When you declare a constant variable, you must assign a value to it:
Give an example
Like this: const int minutesPerHour = 60;
However, this will not work
const int minutesPerHour; minutesPerHour = 60; // Error
finally
This article about learning to use variables in C++ : from definition to practice is introduced to this article, more related to the use of variables in C++ please search the previous articles of the script home or continue to browse the following related articles hope that you will support the script home in the future!
- C++ examples analyze the use of inline functions with reference variables and function overloading
- c++ Multithreading Why use conditional variable detail
- C++ Multi-threading lock and condition variable usage tutorial
- Summary of C++ static member variables and static member functions
- C++ interview question number a, b value interchange (do not use intermediate variables)
- Examples explain the use of variable parameters and default parameters in C++ functions
- C++ does not use variables to find string length strlen function implementation
Related article
-
C++ to achieve patient medical management system
This article mainly introduces in detail the C++ language to achieve patient medical management system, which has certain reference value, interested partners can refer to it2019-01-01 -
C explores the use of dynamic memory allocation in depth
How much space to allocate to the array? Do you have such a question as I did when I first learned C? This installment will talk about dynamic memory allocation, after reading this article, you may have a better understanding of memory allocation2022-04-04 -
Pure C language: Greedy Prim algorithm generation tree problem source code sharing
This article mainly introduces the source code of the greedy Prim algorithm generating tree problem, and friends in need can refer to it2014-01-01 -
C language to achieve student information management program
This article mainly introduces the C language to realize the student information management program in detail, which has certain reference value, interested partners can refer to it2019-03-03 -
Opengl ES FBO frame buffer object usage detail
This article mainly introduces the Opengl ES FBO frame buffer object use details,2022-09-09
in need of friends can use for reference, I hope to be helpful, I wish you a lot of progress, early promotion and pay rise -
The volatile keyword in C++ and its role
This article introduced the volatile keyword in C++, which is used to identify variables that may be accidentally modified and that the compiler should not optimize. This article describes the functions and usage of the keyword volatile through specific code examples, helping readers better understand the application scenarios and implementation principles of the keyword in C++2023-04-04 -
C Binary search method of classical algorithm
This article mainly introduces the relevant information of the binary search method of the C classical algorithm, here provide two methods to help you achieve such a function, the need for friends can refer to2017-10-10 -
About C++.cpp file operation process
This article mainly introduces the whole process of C++.CPP file operation, has a good reference value, I hope to help you. If there are mistakes or incomplete areas, please feel free to comment2023-02-02 -
C++ dynamic programming implements finding the longest common subsequence
This article focuses on the longest common subsequence of C++ dynamic programming, where you want to maximize an indicator. In this example, you want to find the longest common subsequence2022-06-06 -
How does C++ call python and get a return value
This article mainly introduces C++ how to call python and take the return value of the problem, has a good reference value, I hope to help you, if there are mistakes or not fully considered the place, hope to be free to advise2024-03-03
Latest comments