C variables
A variable is actually just the name of the storage area that the program can operate. Each variable in C has a specific type. The type determines the size and layout of the variable storage. Values within this range can be stored in memory, and operators can be applied to the variable.
The name of a variable can consist of letters, numbers, and underscore characters. It must start with a letter or underscore. Uppercase and lowercase letters are different because C is case-sensitive. Based on the basic types explained in the previous chapter, there are the following basic variable types:
Type | Description |
---|---|
char | is usually an octet (one byte). This is an integer type. |
int | The most natural size of an integer for a machine. |
float | Single precision floating point value. |
double | Double precision floating point value. |
void | indicates the absence of type. |
The C language also allows the definition of various other types of variables, such as enumerations, pointers, arrays, structures, unions, etc. This will be explained in subsequent chapters. In this chapter we first explain the basic Variable type.
Variable definition in C
Variable definition tells the compiler where to create variable storage and how to create variable storage. A variable definition specifies a data type and contains a list of one or more variables of that type, as follows:
type variable_list;
Here, type must be a valid C data type , can be char, w_char, int, float, double, bool or any user-defined object, variable_list can consist of one or more identifier names, and multiple identifiers are separated by commas. Several valid declarations are listed below:
int i, j, k;char c, ch;float f, salary;double d;
Line int i, j, k; declares and defines the variables i, j, and k, which instructs the compiler to create an int Variables named i, j, k.
Variables can be initialized (specify an initial value) when declared. An initializer consists of an equal sign, followed by a constant expression, as shown below:
type variable_name = value;
Here are a few examples:
extern int d = 3, f = 5; // d 和 f 的声明, 这就是单纯的声明int d = 3, f = 5; // 定义并初始化 d 和 fbyte z = 22; // 定义并初始化 zchar x = 'x'; // 变量 x 的值为 'x'
Definition without initialization: with static storage duration variables will be implicitly initialized to NULL (the value of all bytes is 0), and the initial value of all other variables is undefined.
Variable declaration in C
Variable declaration assures the compiler that the variable exists with the specified type and name, so that the compiler can proceed further without knowing the complete details of the variable. Compile. Variable declarations only have meaning at compile time; the compiler requires the actual variable declarations when the program is linked.
There are two situations for the declaration of variables:
1. One requires the creation of storage space. For example: int a has already created a storage space when it is declared.
2. The other is that there is no need to create a storage space, by using the extern keyword to declare the variable name without defining it. For example: extern int a where variable a can be defined in other files.
Unless there is the extern keyword, it is the definition of a variable.
extern int i; //声明,不是定义int i; //声明,也是定义
Example
Try the following example, in which the variables have been declared in the header, but are defined and initialized in the main function:
#include <stdio.h>// 变量声明extern int a, b;extern int c;extern float f;int main (){ /* 变量定义 */ int a, b; int c; float f; /* 初始化 */ a = 10; b = 20; c = a + b; printf("value of c : %d \n", c); f = 70.0/3.0; printf("value of f : %f \n", f); return 0;}
When the above code is compiled and executed, it will produce the following results:
value of c : 30value of f : 23.333334
Lvalues (Lvalues) and rvalues (Rvalues) in C
There are two types in C Type of expression:
Lvalue (lvalue): An expression that points to a memory location is called an lvalue expression. An lvalue can appear to the left or right of the assignment number.
Rvalue (rvalue): The term rvalue (rvalue) refers to a numerical value stored at some address in memory. An rvalue is an expression that cannot be assigned, that is, an rvalue can appear to the right of the assignment number, but not to the left.
Variables are lvalues, so they can appear on the left side of the assignment number. Numeric literals are rvalues, so they cannot be assigned and cannot appear on the left side of the assignment number. The following is a valid statement:
int g = 20;
But the following is not a valid statement and will generate a compile-time error:
10 = 20;