Getting Started with C++ - Variables

Column introduction: Why do I want to re-introduce the relevant knowledge of C++. Before that, my understanding of C++ was only superficial. In the later contact with C++, the C++ programming language made me feel more and more esoteric, so I still want to create a new column to introduce C++. For the introduction of C++, this column will first introduce the c++ knowledge of the 98th edition, and then introduce the c++ knowledge of the 11th edition.

Daily sharing: Work hard every day, not for anything else, just to have more choices in the future, choose comfortable days, and choose people you like!

 

Table of contents

1.1. Variable definition

1.1.1. Initial value

1.1.2. List initialization

1.1.3. Default initialization

1.2. Variable declaration and definition

1.3. Identifier

1.4, the scope of the name


The concept of a variable is simply a storage space with a name that can be manipulated by the programmer. Since C++ is a static compiled language, the data type of the variable must be clearly specified when defining the variable, so that the program can allocate the appropriate memory space when compiling.

1.1. Variable definition

The basic format of a variable definition is: first, a type specifier, and then a list of variable names. Variables can also be assigned initial values. Each variable of the same type is separated by a comma, and the end is separated by a semicolon.

int i=-30; 
std::string name("Tom"); //name通过一个string字面值初始化

Notice:

In C++, there are two terms, object and variable. Many people can't tell the difference between an object and a variable. An object refers to a piece of storage space that can store data and has a certain type. By definition alone, they are all one thing. It's just that some people are used to calling objects related to classes, but they can actually be called objects. How to call them depends entirely on personal preferences. It's like the distinction between methods and functions .

1.1.1. Initial value

When an object acquires a specific value when it is created, we say that the object is initialized . For the definition of variables, from the moment of definition, the scope class is in effect.

double price=10.98,Alcount=price*3;
//初始化格式正确,price从初始化开始就已经生效。
double sum=add(price,Alcount);
//调用函数add,将函数计算的值的返回给变量,正确。

It should be noted here that for the problem of using functions to initialize, we will talk about constant expressions later, there will be a little conflict here, and we must distinguish the difference between the two. One is the difference between assignment and initialization . Although it will not affect the writing of programs, it can be understood as a piece of knowledge. Assignment refers to erasing the current value of an object (variable) and replacing it with a new value, while initialization refers to assigning an initial value when creating a variable. There is an essential difference between the two.

1.1.2. List initialization

For the initialization of objects (variables), C++ provides a variety of initialization methods. Regarding these initialization methods, it is better if you can master all of them. As for which one you like, it depends on your personal preference.

int untiled=0;
int untiled={0};
int untiled{0};
int untiled(0);

Among these initialization methods, I recommend list initialization ({}). Although list initialization has limitations, it can help us improve the rationality and accuracy of the code.

double first=3.1415926;
int second{first},third={first};

In the case above, since there may be data loss, using list initialization will cause an error. If you use other initialization methods, there will be no problem:

double first=3.1415926;
int second(first),third=first;

1.1.3. Default initialization

When it comes to variable initialization, we must talk about a problem, that is, when you define a variable without initialization, when you output its value, strange values ​​will appear, and even errors will appear. This is because There is a default initialization in c++ . In any function body, if the variable is not artificially initialized, the variable will not be initialized, but the variable defined outside any function body is initialized to 0.

Notice:

Although C++ has a default initialization mechanism, it is still recommended to initialize variables, so as to ensure the safety of the program and develop good programming habits.

1.2. Variable declaration and definition

Regarding the issue of variable declaration and definition, many friends who are just getting started don’t know it very well, because when you write code, you don’t use too many files. Variables are defined in cpp files, even if two files need to be used The same variable value, many people just redefine a variable. But in C++, there is a function of declaring (extern).

In C++, declarations and definitions are distinguished. The declaration makes the name known to the program. If a file wants to use a variable in another file, it must contain the declaration of that variable. The definition is responsible for creating the entity associated with the name.

The declaration of the variable specifies the type and name of the variable, but does not declare the storage space, while the definition opens up the memory space for the variable.

extern int i; //声明i而非定义i
int i=100; //声明且定义i

When using extern , be careful not to assign an initial value to the variable. Once initialized, it is no longer a declaration, but a definition. Therefore, once a variable is declared, do not display initialization for this variable, because this will cause extern to lose its effect.

Notice:

Variables can only be defined once, but can be declared multiple times.

1.3. Identifier

C++ representations consist of letters, numbers and underscores. Numbers cannot be used as the beginning, only letters and underscores can be used as the beginning.

When defining an indicator, we should follow the corresponding rules, so as to make the code more regular.

Rules for defining notators:

1. Try to use English words or abbreviations, and you must see the name and understand the meaning.

2. Variables generally start with lowercase letters, and class names start with uppercase letters.

3. For combined words, separate them with an underscore.

1.4, the scope of the name

Scope refers to a scope where variables or objects, functions, etc. have functions, and is usually bounded by {} bits. Regarding the concept of scope, there is no difficulty. You only need to know that entities such as variables (objects) and functions have functions only within the corresponding scope.

for example:

#include<iostream>
int sclean=40; //全局变量,作用于全局作用域
int main(){

//进入作用域
{
int i=10; //这里的i只能在这里使用,超出作用域就不可使用;
std::cout<<i<<std::endl;
}
int i=100; //这里可以二次定义,因为前面定义的i已经失效
std::cout<<i<<std::endl;
//int sclean=34; //这里错误,函数体内的变量不能与全局变量重名。
return 0;
}

The program in Sa and um explained the concept of scope very well. Of course, in addition to this, there are scopes in loops such as while and for, which are not bad.

Guess you like

Origin blog.csdn.net/qq_59931372/article/details/132525042