博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++变量_C ++变量
阅读量:2507 次
发布时间:2019-05-11

本文共 7179 字,大约阅读时间需要 23 分钟。

c++变量

In this section we will be studying the concepts of variables in C++ programming language. We hope you guys must have heard about or somewhere else. Though it is not a very complex topic to discuss in detail but let’s see what we have to say for this.

在本节中,我们将研究C ++编程语言中变量的概念。 我们希望你们一定听说过或其他地方的 。 尽管讨论不是一个非常复杂的话题,但让我们看看我们要说些什么。

C ++变量 (C++ Variables)

We can see a variable as a ‘source of storage with some name’, which our programs can use for modification or manipulation. There is always a type associated with each variable which we need to specify at the starting of the program during variable declaration or we can also specify the type of the variable during initialization of that particular variable.

我们可以将变量视为“具有某些名称的存储源”,我们的程序可以将其用于修改或操作。 总是有与每一个我们需要指定在程序的变量声明在起动或我们也可以是特定变量的初始化期间指定变量的类型变量相关联的类型

The type of a variable determines the size of the variable’s memory ( i.e. how many bits can be accommodated within), layout of the memory, range of values that can be stored and the set of instructions that are permissible to be applied over the variable.

变量的类型决定了变量的内存大小(即,可以容纳多少位),内存的布局,可以存储的值的范围以及允许在变量上应用的指令集。

Things to keep in mind when naming a variable:

命名变量时要记住的事情:

Digits, letters and an underscore character is what allowed in the nomenclature of the variables whereas the variables name must start with a letter or an underscore only. C++ is a case-sensitive language so the uppercase and lowercase letters are not the same.

数字,字母和下划线字符是变量名称中允许的字符,而变量名称必须仅以字母或下划线开头。 C ++是区分大小写的语言,因此大写字母和小写字母不同。

Now let’s see what are the some basic types of variable which we can take into consideration in C++:

现在,让我们看看在C ++中可以考虑的一些基本类型的变量:

Type Description
int tells machine that variable is integer type
char character type variable. Takes one byte or called single octet
bool Boolean type which takes two values only, either ‘true’ or ‘false’
float floating type variable (takes single-precision floating value)
wchar_t This stands for ‘wide character’ type
double double type variable (takes double-precision floating value)
void signifies the absence of any variable type
类型 描述
整型 告诉机器变量是整数类型
烧焦 字符类型变量。 占用一个字节或称为单个八位字节
布尔 布尔类型,仅接受两个值,即“ true”或“ false”
浮动 浮点型变量(采用单精度浮点值)
wchar_t 这代表“宽字符”类型
双精度型变量(采用双精度浮点值)
虚空 表示不存在任何变量类型

Moving on now we will study how to define, declare and use different types of variables in C++ language. Come let’s explore.

现在继续,我们将研究如何使用C ++语言定义,声明和使用不同类型的变量。 快来探索吧。

如何在C ++中定义变量 (How to Define a Variable in C++)

While defining a variable in any programming language we tell the compiler where and how much space should be created for the variable. In variable definition the type of the variable and a list of one or more variables must be specified something like this:

在使用任何编程语言定义变量时,我们告诉编译器应在何处以及为变量创建多少空间。 在变量定义中,必须指定变量的类型以及一个或多个变量的列表,如下所示:

type variable_list;

Here type is a C++ data type that can be anything from int, char, bool, double etc. or even any user-defined data type. And the variable_list signifies one or more comma separated variable names. Let’s understand this with a code excerpt:

这里的type是C ++数据类型,可以是int,char,bool,double等任何类型,甚至可以是任何用户定义的数据类型。 并且variable_list表示一个或多个逗号分隔的变量名。 让我们用一段代码摘录来理解这一点:

int a, c, num;char c, x1, x2;float price, salary;

In the first line we declared and defined the variables a, c & num, which instructs the compiler to create three variables each of type int.

在第一行中,我们声明并定义了变量a,c和num,这指示编译器创建三个类型均为int的变量。

如何在C ++中初始化变量 (How to Initialize a Variable in C++)

Initialization basically means ‘assignment of the value’. The variable initialization can be done at the time of declaration of the variable or somewhere else in the program. Something like this:

初始化基本上意味着“价值的分配”。 变量初始化可以在声明变量时或在程序中的其他地方进行。 像这样:

type variable_name = value;

As we can infer from above that an assignment operator ( = ) is used for the initialization of the variable. Let’s have some examples to understand this better:

从上面可以推断出,赋值运算符(=)用于变量的初始化。 让我们看一些例子来更好地理解这一点:

int a = 10;                          // defining and initializing variable afloat b = 2.0;                  // defining and initializing variable bchar c = ‘x’;                   // defining and initializing variable c

We can visualize the variable a in memory as given below.

我们可以如下所示在内存中可视化变量a。

Variables in C++

Note: Keep in mind that you can declare your variable in the program as many times as you want and anywhere, but the variables can be defined only once in the file, a function or a code excerpt.

注意:请记住,您可以在程序中随时随地声明变量,但是变量只能在文件,函数或代码摘录中定义一次。

如何在C ++中声明变量 (How to Declare a Variable in C++)

The declaration of the variable is for the purpose of letting compiler know that there exist a variable with given type and given name so that compiler can do it’s work i.e. further compilation without worrying about more detail of that variable.

声明变量的目的是让编译器知道存在一个具有给定类型和给定名称的变量,以便编译器可以执行其工作,即进一步编译而不必担心该变量的更多细节。

At the time of linking of the program, the compiler do not need the variable declaration, the variable declaration does the work for it. We can infer from here that variable declaration is needed at the time of compilation only. We have seen how to declare a variable above, now let’s see an example of this:

在链接程序时,编译器不需要变量声明,变量声明为它工作。 从这里我们可以推断出,仅在编译时才需要变量声明。 我们已经看到了如何在上面声明变量,现在让我们看一个例子:

#include
 using namespace std; int main(){    int a, b;    int c;     a = 3;    b = 4;    c = a * b;     cout << c;     return 0;}

Output

输出量

12

12

Above program does multiplication of two numbers. 

上面的程序将两个数字相乘。  

局部变量 (Local Variables)

The variables that are declared inside a function or a block of code are considered as local variables. Theses variables are only confined to be used by the statements that lie within their own function or code block. The statement or function lying outside the block are not allowed to have the access to the local variable defined in other block.

在函数或代码块中声明的变量被视为局部变量。 这些变量仅限于由位于其自己的函数或代码块中的语句使用。 位于该块外部的语句或函数不允许访问其他块中定义的局部变量。

In above examples of multiplication of two numbers, a, b and c where local variables because they were declared inside main() function.

在上面两个数字相乘的示例中,a,b和c是局部变量,因为它们是在main()函数内部声明的。

全局变量 (Global Variables)

The variable that is defined outside all blocks and function and can be accessed by all of them is known as global variable. Global variable is usually defined at the top of the program and is available to all of the functions and blocks as long as program is into existence. And the value of the global variable is retained throughout the life of the program. Let’s have an example of code to understand this concept practically: 

在所有块和功能之外定义并且可以被所有块和函数访问的变量称为全局变量。 全局变量通常在程序顶部定义,并且只要程序存在就可用于所有功能和块。 全局变量的值将在程序的整个生命周期中保留下来。 让我们举一个代码示例来实际理解这个概念:  

#include
 using namespace std; int a;  //global variable int main(){    int b, c;   //local variables  b = 3; c = 5; a = b * c;  cout << a;  return 0;}

Output

输出量

15

15

Note: If there exist any such condition that both the global and the local variable have same name, then in such cases local variable is given preference over global variable.

注意:如果存在这样的条件,即全局变量和局部变量都具有相同的名称,则在这种情况下,局部变量优先于全局变量。

Comment below if you have queries related to above C++ variables tutorial.

如果您对以上C ++变量教程有疑问,请在下面评论。

翻译自:

c++变量

转载地址:http://ymggb.baihongyu.com/

你可能感兴趣的文章
Python内置函数(19)——eval
查看>>
怎样录制屏幕并将结果保存为Gif
查看>>
别名设置 alias
查看>>
练习3.34
查看>>
oracle加减操作
查看>>
dp乱写3:环形区间dp(数字游戏)
查看>>
【Beta阶段】启程会议——第零次Scrum Meeting!
查看>>
Apple Tree
查看>>
JS 中对变量类型的五种判断方法
查看>>
学习进度十五
查看>>
解决Android Studio启动项目后一直处于refreshing 'View' gradle project,快速解决亲测有效...
查看>>
4.12 | 学习笔记
查看>>
python开发【第一篇】:基础知识
查看>>
javascript的window.onload()方法和jQuery的$(document).ready()的对比
查看>>
mysql数据库维护(备份和还原)和性能提高
查看>>
第八章 springboot + mybatis + 多数据源
查看>>
Arab and North African Region,2002(Snakes & ladders)
查看>>
React中的Refs
查看>>
自己使用MySQL中的GROUP_CONCAT(CONCAT_WS())函数查询的数据显示不全的问题. 以及在后台开发中怎么设置使用....
查看>>
Mysql强制修改密码
查看>>