What does the definition of C language variables include variable storage type and variable?

青灯夜游
Release: 2020-07-27 14:39:45
Original
4871 people have browsed it

The definition of C language variables includes variable storage type and variable name. The format of defining variables in C language: "data type variable name;", "data type" indicates what type of data you want to store, and "variable name" is what name you want to give the variable, usually using letters.

What does the definition of C language variables include variable storage type and variable?

Definition of variables

The format for defining variables is very simple, as follows:

数据类型 变量名;
Copy after login

The first thing to emphasize is: never lose the final semicolon. The definition of a variable is a statement. As we said, statements end with a semicolon.

"Data type" indicates what type of variable you want to store. If you want to store integers, define it as int type; if you want to store decimals, define it as float or double type; if you want to store characters, define it as char type...

The "variable name" is what you want to give this variable Whatever the name is, it usually uses letters. For example:

int i;
Copy after login

means that an integer variable i is defined.

Naming rules for variable names

When defining a variable, the variable name can be a combination of letters, numbers, and underscores. But it is not a random combination. Please pay attention to the following naming rules:

1) The beginning of the variable name must be a letter or an underscore, not a number. The most commonly used names in actual programming start with a letter, while variable names starting with an underscore are system-specific.

Open any header file and you will see that all variable names, macro names, and function names in it all start with an underscore.

So in order to avoid conflicts with system-defined names, when programming, never use an underscore as the beginning of a variable name unless it is required to be defined in this way.

2) Letters in variable names are case-sensitive. For example, a and A are different variable names, and num and Num are also different variable names.

3) Variable names must not be C language keywords, this must be remembered!

4) There cannot be spaces in variable names. This can be understood like this: Because we said above, the variable name is a combination of letters, numbers, and underscores, without spaces.

Assignment of variables

So how do you put the number into the variable after the variable is defined? The first thing to understand is that putting a number into a variable is called "assignment". "Assign" means "give", so "assigning a value to a variable" means passing a value to a variable. How to assign value? It is through the assignment operator =. The format of the assignment is:

变量名 = 要赋的值;
Copy after login

It means assigning the number on the right side of = to the variable on the left. For example:

i = 3;
Copy after login

This means that 3 is assigned to variable i, and i is equal to 3 at this time.

It should be noted here that the = here is different from the "equal sign" in mathematics. When you first start learning C language, it is difficult for everyone to change from mathematical thinking at this point. In C language = means assignment, that is, assigning the value on the right to the variable on the left, rather than the variable on the left being equal to the value on the right.

Is there an equal operator in C language? This is the double equal sign ==. This operator has the same meaning as "equal" in mathematics.

The above is the definition and assignment of variables, which are written in two steps. They can also be combined into one step, and in fact, the combination of two into one is the most commonly used method in actual programming. So how do you put it together? Very simple, the form is as follows:

Data type variable name = value to be assigned;

For example:

int i = 3;
Copy after login

means that a variable i is defined and 3 is assigned to this variable. It is equivalent to

int i; i =3;
Copy after login

.

When defining variables, you can also define multiple variables at once, for example:

int i, j;
Copy after login

This means that variables i and j are defined. What needs to be emphasized here is that when multiple variables are defined at the same time, the variables are separated by commas and must not be written as semicolons. This is the most common mistake that many novices make, that is, confusing commas and semicolons.

You can also define multiple variables and assign values to them at the same time:

int i = 3, j = 4;
Copy after login

Still separate them with commas, and don’t forget to enter a semicolon at the end.

Finally, it should be noted that in the older C89/C90 standard (also known as the ANSI C standard), variables can only be defined at the beginning of the program, or there cannot be other non-declarations or variables in front of the variable definition. non-defining statement. For example, in C language, the following writing is wrong:

# include  int main(void) { int i; i = 3; int j = 4; //这句是错误的,因为在它前面有一个给变量i赋值的非定义语句 return 0; }
Copy after login

When compiling under VC 6.0 or VS2010, the following error will be prompted:

error C2143: 语法错误 : 缺少“;”(在“类型”的前面)
Copy after login

But this writing is in .cpp It is allowed in files (C source files), and variables can be defined anywhere in the program in .cpp files.

In fact, this has nothing to do with the language itself. It does not mean that variables in C language cannot be defined in the middle of the program. This is actually related to the standard. Some compilers use the C89 standard. The C89 standard requires that all declarations (such as function declarations, variable definitions) must be written at the beginning of the program, function or compound statement, and the new features of C99 Statements and declarations are allowed in any order, as long as the "declare first, use later" principle is followed.

Because VC 6.0 and VS2010 follow the C89 standard, or it cannot fully support the C99 standard, it does not allow variables to be defined in the middle of the program; and the GCC compiler in Linux follows the C99 standard, so It is also a .c file and written in C language. It can define variables at any location in the program, as long as it is before the location of use.

In fact, the C99 standard is better. Variables are defined only when needed, which shortens its life cycle and saves memory. Because as long as a variable is defined, the system will allocate memory space for it, so if it is defined earlier but not used until the end, it will occupy the memory until it is used, which is a bit wasteful.

But despite this, everyone should try to write according to the C89 standard when programming. Even in a compiler that perfectly supports the C99 standard, it is best not to write declarations in the middle of the program. Because C89 has been around for a long time, it is still mainstream and many compilers still only support the C89 standard or cannot support the C99 standard well. Therefore, writing according to the C89 standard is more portable.

As mentioned above, "compound statements" can also be used to define variables in compound statements. The so-called statement ends with a semicolon, and a compound statement is a statement that combines multiple statements together. In C language, multiple statements enclosed by braces { } are called compound statements.

Compound statements are used a lot in process control. For example, if, for, and while that we will learn later are usually followed by compound statements. At this time, variables can also be defined in these compound statements, but only at the beginning. The main function main is also enclosed in curly brackets, so the function body under the main function main is essentially a compound statement, a large compound statement.

So they still have something in common, that is to say, in C language, as long as it is enclosed by curly brackets { }, variables can be defined at the beginning. And these variables defined in curly brackets are called local variables. In addition, variables can also be defined outside curly braces, called global variables.

Recommended: "c Language Tutorial"

The above is the detailed content of What does the definition of C language variables include variable storage type and variable?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!