Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is the definition of variable in C language?
What is the definition of variable in C language?
Variables are often used in C language. What are the variables in C language? Then the following is the definition of C language variables that I compiled myself. Welcome to reading.

What are the variables in C language? Variable, which comes from mathematics, is an abstract concept in computer language. It can store calculation results or represent numerical values. Variables can be accessed by their names. In imperative languages, variables are usually variable; But in a purely functional language (such as Haskell), variables may be immutable. In some languages, variables may be clearly defined as abstractions that can represent the state of variables and have storage space (such as in Java and Visual Basic). But other languages may use other concepts (such as object of C) to refer to this abstract without strict definition? Variable? The exact extension of.

Variable introduction

Variables are very useful because they allow you to give each piece of data used in a program a short and memorable name. Variables can save the data input by users when the program is running (for example, use the InputBox function to display a dialog box on the screen, and then save the text typed by users into variables), the result of a specific operation, and a piece of data to be displayed on the form. In short, variables are simple tools for tracking almost all types of information.

If there is no assignment after the variable is declared, the compiler will automatically prompt and give the default value.

Variable is a convenient placeholder to refer to the computer memory address, which can store program information that can be changed when the script runs. For example, you can create a variable named Click Count to store the number of times a user clicks an object on a web page. You don't need to know the address of the variable in the computer memory to use this variable. You can view or change the variable value by referencing the variable name. In VB script, there is only one basic data type, namely Variant, so the data types of all variables are Variant.

Declare variables

One way to declare variables is to explicitly declare variables in scripts using Dim statements, Public statements and Private statements. For example:

A dim Fahrenheit temperature

When declaring multiple variables, use commas to separate the variables. For example:

The top, bottom, left and right sides are dimmed.

Another method is to declare variables implicitly by using the variable name directly in the script. This is usually not a good habit, because sometimes the misspelled variable name will lead to unexpected results when running the script. Therefore, it is best to use the Option Explicit statement to explicitly declare all variables as the first statement of the script.

Naming rules

First of all, we should give the variable a proper name, as if everyone has his own name, otherwise it will be difficult to distinguish. In VB6, the naming of variables must follow the following rules:

(1) Variable names must start with letters or underscores, and only letters, numbers and underscores can be used in the middle of the name? _? Composition; The last character can be a type descriptor;

(2) The length of variable name shall not exceed 255 characters;

(3) Variable names must be unique within the valid range. Effective range refers to the range in which a program can identify and use reference variables? Such as process, form, etc. The range of reference variables will be introduced later.

(4) Variable names cannot be reserved words (keywords) in VB, nor can they be reserved words with type descriptors at the end, but they can be embedded in variable names. Keywords refer to the system internal identifiers such as attributes, events, methods, procedures and functions in VB6. Such as defining words (if, endif, while, loop, etc. ) and function names (len, format, msgbox, etc. Like Print, Print$ is illegal, but Myprint is legal. For example: strName 1, intMax_Length, interless, strNo3, etc. Is a legal variable name, and a &;; B, ok, 3M, _Number, etc. Is an illegal variable name.

note:

(1) Variable names in VB are case-insensitive (such as abc, aBc, ABC, etc.). Case sensitivity in. c language. Different languages have different rules.

(2) When defining and using variables, it is usually necessary to define the variable name as a name that is easy to use and read, and can describe the usefulness of the data contained, instead of using some difficult abbreviations such as A or B2. For example, suppose you are laying out a software that sells apples for fruits. We need two variables to store Apple's price and sales. At this point, you can define two variables named Apple_Price and Apple_Sold. Every time you run the program, the user will provide specific values for these two variables, which looks very intuitive. The specific method is to form a meaningful variable name with one or more words to make the meaning of the variable clear. For example, the meaning of the variable name SalesTaxRate is much clearer than that of Tax or Rate.

(3) Mix uppercase and lowercase letters and numbers as needed. A reasonable protocol is to capitalize the first letter of each word in a variable, such as DateOfBirth.

(4) Another reasonable protocol is that each variable name starts with two or three character abbreviations, corresponding to the data type of the data to be stored in the variable. For example, use strName to explain that the Name variable holds string data. This naming method is called Hungarian nomenclature.

Format Variable Type+Variable Name

For example, just now, strName“str "is the abbreviation of" string "and" Name "is the name of a variable.

Note that the variable types are all lowercase, and the beginning of the variable name is uppercase.

C# naming method

1

stringstrName

VB naming method

1

DimstrNameasString

Although you don't need to pay too much attention to the details of character abbreviations, you should look at the conventions in this regard in the future. Detailed information about this convention can be found in Visual Basic online help and many books on advanced programming of Visual Basic.

Survival period

The time a variable exists is called lifetime. The life cycle of script-level variables starts from the moment they are declared until the end of script running. For process-level variables, their lifetime is only the time when the process runs, and after the process is over, the variables disappear. Local variables are ideal temporary storage space when executing procedures. Local variables with the same name can be used in different procedures, because each local variable can only be recognized by the procedure that declares it.

sphere of action

The scope of a variable is determined by its declared position. If a variable is declared in a procedure, only the code in the procedure can access or change the value of the variable. At this point, the variable has a local scope and is called a process-level variable. If a variable is declared outside a procedure, it can be recognized by all procedures in the script, and it is called a script-level variable with script-level scope.

all one's life

Refers to the whole process of variable allocation and recovery,

type

Attribute variables and variables created by users themselves.

When we design a user interface in a form, vb6 will automatically create a set of variables, namely attribute variables, for the generated objects (including the form itself) and set their default values for each variable. This variable can be used directly by us, such as referencing it or assigning it a new value.

Users can also create their own variables to store temporary data or result data during program execution. In programs, such variables are very needed. Here's how to create and use such variables.

Declare variables

Before using a variable, it must be declared in the code, that is, the variable is created.

Most languages usually need to declare variables before using them. In other words, you must tell the compiler in advance which variables are used in the program, their data types and lengths. This is because the compiler needs to know how to open the storage area for statement variables before executing the code, so as to optimize the execution of the program.

There are two ways to declare variables: implicit declaration and explicit declaration.

Implicit declaration:

Variables can be used directly without declaration. In this case, VB gives variables a default type and value. This method is simple and convenient. Variables can be named and used at any time in the program code, but they are not easy to check.

Clearly stated:

Create variables with declaration statements.

Force explicit declaration of variables:

In order to avoid the trouble caused by the misspelling of variable names, users can stipulate that VB will issue an error warning whenever it encounters a name that is not explicitly declared as a variable. The method is to force explicit declaration of variables. To force explicit declaration of variables, simply add the following statement to the declaration part of class module, form module or standard module:

Option explicit

This statement is used to stipulate that all variables in the module must be declared before use, that is, variables cannot be created by implicit declaration. After adding the Option Explicit statement, VB will automatically check whether there are undefined variables in the program, and will display an error message when it is found.

If you want to automatically insert the Option Explicit statement, the user only needs to insert it? Tools? Select from the menu? Options? Command, and then click? Options? In the dialog box? Editor? Tab, and then select? Need variable declaration? election

In this way, VB will automatically insert the Option Explicit statement in any new module, but only in the newly established module. Therefore, for the established module, you can only manually add the Option Explicit statement to the existing module (this function will only be effective after VB restarts).

Range of rational variables

The scope of the variable determines the part of the code that knows that the variable exists. When a variable is declared inside a procedure, only the code inside the procedure can access or change its value; It has a scope local to the process. However, sometimes it is necessary to use a wider range of variables, for example, the value of a variable is valid for all processes in the same module, or even for all processes in the whole application. Visual Basic allows you to specify the scope of a variable when you declare it.

Storage type

We often define some variables in the program to save and process data. In essence, a variable represents an operational memory, which can also be considered as a symbolic representation of memory. When a program needs memory, you can define a certain type of variable. At this time, the compiler allocates a certain memory space according to the data type of the variable. Programs can access the corresponding memory through variable names.

If the data type of a variable determines the size of the corresponding memory, then the storage type will affect the use of the corresponding memory. The so-called usage is when and where variables can be used in the program, that is, the life cycle and scope of variables.

Understand some basic common sense first. 1. When the program is running, there are three areas in memory where variables can be stored: static storage area, stack and heap. Second, according to the position of variable definition, it is divided into global variables (variables defined outside the function) and local variables (variables defined inside the function, including formal parameters).

All global variables and static local variables (defined by the keyword static) are stored in the static storage area, which is characterized by allocating memory space and initializing it at compile time. During the running of the program, the variable exists until the end of the program, and the memory space corresponding to the variable is released.

All non-static local variables (also called automatic variables) are stored in the stack, which is characterized by dynamic creation when executing the function or module where the variable is located, and releasing the memory space corresponding to the variable when executing the function or module. In other words, every time a function or module is executed, local variables are redistributed. If the variable is not initialized when it is defined, then the value in the variable is a random number.

All the memory allocated by malloc (also called dynamic memory) is in the heap, and its characteristic is that dynamically allocated memory is generally accessed through pointers. In other words, dynamic memory can be released manually through free or automatically by the system at the end of the program.

The life cycle of variables is discussed above. Let's look at the scope. Scope refers to the visible range of a variable, that is, which parts of a program can use the variable during its life cycle.

The range of global variables starts from the definition point and ends at the source file. If you want to use global variables before defining points, you need to use the keyword extern to extend the scope. By default, global variables can be referenced by other files. If you only want to use it in this document, you need to use the keyword static when defining it.

For local variables, whether static or automatic, the scope is limited to the function or module that defines the variable.

As long as the address of dynamic memory is known, dynamic memory can be used anywhere in the program, as long as it is not released.

Note: Static affects the scope before global variables and the life cycle before local variables.

Variable type

In C language, variables are divided into global variables and local variables. It can also be divided into automatic variables and static variables. The former is divided according to the scope of variables, and the latter is divided according to the storage mode of variables.

If divided by storage space, it can be an integer variable, a character variable, a floating-point variable, etc. Of course, there are arrays, structural variables and so on.

C language also has an important variable: pointer variable. Its stored value is a memory address.

Operating system variable

Operating system variable

Variable names in C language are case-sensitive. For example, sun and SUN are two different variable names.

On the other hand, when declaring variables, you can directly assign values to determine the language of the variable type (JavaScript, before Flash CS 3.0, etc.). The declaration of such language variables is usually called weak type, while (c++ and so on. ) must be declared before use, and the variable type must be determined when declaring, which is a strict data type.

There are two types of variables: attribute variables and user-created variables.

Java Script language

changeable

Just like algebra, JavaScript variables are used to hold values or expressions.

You can give the variable a short name, such as X, or a more descriptive name, such as length.

JavaScript variables can also hold text values, such as carname="Volvo ".

Rules for variable names

Variables are case-sensitive (y and y are two different variables)

Variables must start with a letter or underscore.

Note: Because JavaScript is case-sensitive, variable names are case-sensitive.

example

During the execution of the script, you can change the value of the variable. You can refer to a variable by name to display or change its value.

This example shows you the principle.

Declare (create) JavaScript variables

Creating variables in JavaScript is often called. Statement? Variable.

You can declare JavaScript variables through var statements:

var x; var carname

var x; var carname

After the above declaration, the variable has no value, but you can assign a value to the variable when you declare it:

var x = 5; Var carname = "Volvo";

var x = 5; Var carname = "Volvo";

Note: When assigning a text value to a variable, please enclose the value in quotation marks.

Assign values to JavaScript variables

Assign values to JavaScript variables through assignment statements:

x = 5; Carname = "Volvo";

x = 5; Carname = "Volvo";

The variable name is to the left of the = symbol, and the value assigned to the variable is to the right of =.

After the above statement is executed, the value saved in the variable X is 5, and the value of carname is Volvo.

Assign values to undeclared JavaScript variables

If the variable you assign has not been declared, it will be declared automatically.

These statements:

x = 5; Carname = "Volvo";

x = 5; Carname = "Volvo";

Have the same effect as these statements:

var x = 5; Var carname = "Volvo";

var x = 5; Var carname = "Volvo";

Redeclare JavaScript variables

If you declare a JavaScript variable again, it won't lose its original value.

var x = 5; var x;

var x = 5; var x;

After executing the above statement, the value of variable x is still 5. When a variable is redeclared, the value of x is not reset or cleared.

JavaScript algorithm

Just like algebra, you can use JavaScript variables to do arithmetic:

y = x-5; z = y+5;

y = x-5; z = y + 5

Variable types in php

Variables in PHP: Variables are used to store values, such as numbers, text strings or arrays.

Once the variable is set, we can reuse it in the script.

All variables in PHP start with a $ sign.

The correct way to set variables in PHP is:

$ var _ name = valuePHP Beginners will forget the $ sign in front of variables. If you do this, the variable will be invalid.

We create a string variable and a numeric variable:

You don't have to declare the data type of this variable to PHP.

Depending on how the variable is set, PHP will automatically convert the variable to the correct data type.

In strongly typed programming languages, you must declare the type and name of variables before using them.

In PHP, variables are automatically declared when they are used.

Variables in php are represented by a dollar sign followed by the variable name. Variable names are case-sensitive.

Variable names follow the same rules as other tags in php. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers or underscores. According to the normal regular expression, it will be expressed as:' [a-za-z _/x7f-/xff] [a-za-z0-9 _/x7f-/xff] *'.

Note: The letters mentioned here are a-z, A-Z and ASCII characters of 127 to 255(0x7f-0xff).

In php 3, variables are always assigned values. That is to say, when the value of an expression is assigned to a variable, the value of the whole original expression is assigned to the target variable. This means that, for example, when the value of one variable is assigned to another variable, changing the value of one variable will not affect the other variable. For this type of assignment operation, please refer to the chapter on expressions.

Php 4 provides another way to assign values to variables: reference assignment. This means that the new variable is just referenced (in other words, becomes its alias? Or? Point? ) the original variable. Changing the new variable will affect the original variable, and vice versa. This also means that no copy operation is performed; Therefore, this assignment operation is faster. However, it is only in dense loops or when assigning values to large arrays or objects that it is possible to notice the increase in speed.

Using reference assignment, just put a&; This symbol is added before the variable to be assigned (source variable). For example, the following code snippet will be output. My name is Bob? Twice:

It should be pointed out that only variables with names can refer to assignments.

Naming rules for variables:

Variable names must start with a letter or underscore "_".

Variable names can only contain alphanumeric characters and underscores.