C + + variables (c + + variable definition, variable assignment, naming rules)

Source: Internet
Author: User
Tags sin

In fact, the variables have been used many times in the previous example. The amount of value that can be changed while the program is running is called a variable. A variable should have a name and occupy a certain storage unit in memory, where the value of the variable is stored. Note the two different concepts of distinguishing between variable names and variable values, as shown in Figure 2.6.


Figure 2.6 Variable name rules

The concept of identifiers is introduced first. As with other high-level languages, valid character sequences used to identify entity names such as variables, symbolic constants, functions, arrays, types, and so on are called Identifiers (identifier). Simply put, an identifier is a name. The variable name is one of the identifiers, and the name of the variable must follow the naming rules of the identifier.

C + + Specifies that identifiers can only consist of letters, numbers, and underscores of 3 characters, and the first character must be a letter or an underscore. The following list is a valid identifier and is a valid variable name:
Sum, average, total, day, month, Student_name, Tan, BASIC, li_ling

The following is an illegal identifier and variable name:
M.d.john, $123, #33, 3g64, Ling Li, C + +, zhang-ling, U.S.A.

Note: in C + +, uppercase and lowercase letters are considered to be two different characters. As a result, sum and sum are two different variable names. In general, variable names are expressed in lowercase letters, consistent with people's daily habits to increase readability. Note that the variable name cannot be the same as the C + + keyword, system function name, and class name. In foreign software development work, it is customary to prefix a variable with a letter to indicate the type of the variable, such as icount indicates that this is an integer variable, and csex indicates that it is a character variable.

C + + does not specify the length of the identifier (number of characters), but each specific C-compiler system has its own rules. Some systems take 32 characters, and the characters that exceed them are not recognized.

Defining variables

In the C + + language, all variables used are required to be defined, that is, must be "defined first, then used", as in example 2.2 and example 2.3. The general form of defining variables is:
Variable type variable name table column;
A Variable Name table column refers to a sequence of one or more variable names. Such as:
float a,b,c,d,e;
Define A,b,c,d,e as a single-precision variable, noting that each variable is separated by commas, and finally a semicolon. You can specify the initial value of a variable when it is defined. Such as:
Float a=83.5, B, c=64.5, d=81.2, E; The initial value is specified for the variable a,c,d, and B and D do not specify an initial value

The C language requires that the definition of a variable be placed before all execution statements, while C + + relaxes the limit and only requires that the variable be defined before the first time it is used. In other words, it can appear in the middle of a statement, such as:
int A; Define variable a (defined before using a)
A=3; Executes the statement, assigning a value to a
float B; Define variable B (defined before using B)
b=4.67; Execute statement, assign value to B
char c; Define variable C (defined before using C)
c= ' A '; Execute statement, assign value to C

C + + requires that variables be coerced to define the purpose of:
1) where the variable name is not defined in advance, it will ensure that the variable names in the program are used correctly. For example, if you write in the Declarations section
int student;
In the execution statement, the error is written as statent. Such as
statent=30;
At compile time check out statent undefined, as error handling. Output "Variable statent undeclared" information to facilitate users to find errors, avoid error when using variable names.

2) Each variable is designated as a deterministic type and can be assigned a corresponding storage unit at compile time. If you specify a and b as int, the general compilation system allocates 4 bytes each, and stores the data as integers.

3) specifies that each variable belongs to a specific type, which makes it easier to check the validity of the operation of the variable at compile time. For example, integer variables A and B can be used for redundancy operations:
A%b
% is "redundancy" to get the remainder of a/b. If A and B are specified as real variables, the "redundancy" operation is not allowed and the error information is given at compile time.

Assigning an initial value to a variable

Allows you to assign an initial value to a variable when it is defined, which is called a variable initialization. The initial value can be a constant, or it can be an expression that has a definite values. Such as
Float A, b=5.78*3.5, C=2*sin (2.0);
The a,b,c is defined as a single-precision floating-point variable, B is initialized to 5.78*3, C is initialized to 2*sin (2.0), and the value of the sine function sin (2.0) is obtained from the standard function library after the connection is compiled, so the variable C has a definite initial values. Variable A is not initialized.

If an initial value is not assigned to a variable, the initial values of the variable are unpredictable, that is, the contents of the storage unit are unknown. For example, if you do not assign a value to a and B, the output statement
cout<<a<< "" <<b<< "" <<c<<endl;
The output may result in
1.48544e-38 15 1.81858 (different operating conditions may vary)
Initialization is not done during the compile phase (only static storage variables and external variables that are described in chapter 4th are initialized in the compilation phase), but are assigned to the initial value when the program is run, which is equivalent to executing an assignment statement. For example:
int a=3;
The equivalent of the following two statements:
int A; Specify a as an integer variable
A=3; Assignment statement, assigning 3 to a
Assigning the same initial value to multiple variables must be specified separately and cannot be written as
float a=b=c=4.5;
and should be written
Float a=4.5, b=4.5, c=4.5;
Or
Float A, b, c=4.5;
A=b=c;

C + + variables (c + + variable definition, variable assignment, naming rules)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.