c++中的变量类型
Variable are used in C++, where we need storage for any value, which will change in program. Variable can be declared in multiple ways each with different memory requirements and functioning. Variable is the name of memory location allocated by the compiler depending upon the datatype of the variable.
变量用在C ++中,在这里我们需要存储任何值,这些值将在程序中更改。 可以用多种方式声明变量,每种方式具有不同的内存要求和功能。 变量是由编译器根据变量的数据类型分配的内存位置的名称。
![variables in C++](https://i-blog.csdnimg.cn/blog_migrate/dfb404bfa6133085b9b645c43dc47e2d.png)
变量的基本类型 (Basic types of Variables)
Each variable while declaration must be given a datatype, on which the memory assigned to the variable depends. Following are the basic types of variables,
必须在声明时为每个变量提供一个数据类型,分配给该变量的内存取决于该数据类型。 以下是变量的基本类型,
bool | For variable to store boolean values( True or False ) |
char | For variables to store character types. |
int | for variable with integral values |
float and double are also types for variables with large and floating point values |
bool | 用于存储布尔值的变量(True或False) |
char | 用于存储字符类型的变量。 |
int | 用于带整数值的变量 |
float 和double 也是具有大和浮点值的变量的类型 |
声明和初始化 (Declaration and Initialization)
Variable must be declared before they are used. Usually it is preferred to declare them at the starting of the program, but in C++ they can be declared in the middle of program too, but must be done before using them.
变量必须在使用前声明。 通常,最好在程序启动时声明它们,但是在C ++中,它们也可以在程序中间声明,但是必须在使用它们之前完成。
For example:
例如:
int i; // declared but not initialised
char c;
int i, j, k; // Multiple declaration
Initialization means assigning value to an already declared variable,
初始化是指将值分配给已声明的变量,
int i; // declaration
i = 10; // initialization
Initialization and declaration can be done in one single step also,
初始化和声明也可以一步完成,
int i=10; //initialization and declaration in same step
int i=10, j=11;
If a variable is declared and not initialized by default it will hold a garbage value. Also, if a variable is once declared and if try to declare it again, we will get a compile time error.
如果声明了一个变量且默认未初始化,则它将保存一个垃圾值。 另外,如果一次声明了变量,然后尝试再次声明它,我们将得到编译时错误。
int i,j;
i=10;
j=20;
int j=i+j; //compile time error, cannot redeclare a variable in same scope
变量范围 (Scope of Variables)
All the variables have their area of functioning, and out of that boundary they don't hold their value, this boundary is called scope of the variable. For most of the cases its between the curly braces,in which variable is declared that a variable exists, not outside it. We will study the storage classes later, but as of now, we can broadly divide variables into two main types,
所有变量都有其作用范围,并且超出该范围而不保持其值,此边界称为变量范围。 在大多数情况下,它在花括号之间,其中变量声明存在变量,而不是在变量外部。 我们将在稍后研究存储类,但是到目前为止,我们可以将变量大致分为两种主要类型,
Global Variables
全局变量
Local variables
局部变量
全局变量 (Global variables)
Global variables are those, which ar once declared and can be used throughout the lifetime of the program by any class or any function. They must be declared outside the main()
function. If only declared, they can be assigned different values at different time in program lifetime. But even if they are declared and initialized at the same time outside the main() function, then also they can be assigned any value at any point in the program.
全局变量是那些一旦声明就可以在程序的整个生命周期中被任何类或任何函数使用的变量。 必须在main()
函数外部声明它们。 如果仅声明,则可以在程序生命周期中的不同时间为其分配不同的值。 但是,即使在main()函数之外同时声明和初始化它们,也可以在程序中的任何位置为它们分配任何值。
For example: Only declared, not initialized
例如:仅声明,未初始化
#include <iostream>
using namespace std;
int x; // Global variable declared
int main()
{
x=10; // Initialized once
cout <
局部变量 (Local Variables)
Local variables are the variables which exist only between the curly braces, in which its declared. Outside that they are unavailable and leads to compile time error.
局部变量是仅在大括号之间声明的地方存在的变量。 除此之外,它们不可用并导致编译时错误。
Example :
范例 :
#include <iostream>
using namespace std;
int main()
{
int i=10;
if(i<20) // if condition scope starts
{
int n=100; // Local variable declared and initialized
} // if condition scope ends
cout << n; // Compile time error, n not available here
}
一些特殊类型的变量 (Some special types of variable)
There are also some special keywords, to impart unique characteristics to the variables in the program. Following two are mostly used, we will discuss them in details later.
还有一些特殊的关键字,以赋予程序中的变量独特的特征。 以下两个是最常用的,我们将在后面详细讨论。
Final - Once initialized, its value cant be changed.
最终 -初始化后,其值将无法更改。
Static - These variables holds their value between function calls.
静态 -这些变量在函数调用之间保留其值。
Example :
范例 :
#include <iostream.h>
using namespace std;
int main()
{
final int i=10;
static int y=20;
}
翻译自: https://www.studytonight.com/cpp/variables-scope-details.php
c++中的变量类型