C Variables
A variable is nothing more than a name for a storage area that a program can manipulate. Every variable in C has a specific type, which determines the size and layout of the variable's storage, the range of values that can be stored in memory, and the operators that can be applied to the variable.
A variable name can consist of letters, digits, and underscore characters. It must begin with a letter or an underscore. Uppercase and lowercase letters are distinct 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 | Usually one byte (eight bits); this is an integer type. |
| int | Integer type, 4 bytes, with a range from -2147483648 to 2147483647. |
| float |
Single-precision floating-point value. Single precision has the format: 1 sign bit, 8 exponent bits, and 23 fraction bits.
|
| double |
Double-precision floating-point value. Double precision has 1 sign bit, 11 exponent bits, and 52 fraction bits.
|
| void | Represents the absence of a type. |
C also allows defining variables of various other types, such as enumerations, pointers, arrays, structures, unions, and so on, which will be explained in later chapters. In this chapter, we first cover the basic variable types.
Variable definition in C
A variable definition tells the compiler where and how to create storage for the variable. A variable definition specifies a data type and contains a list of one or more variables of that type, as follows:
type variable_list;
type represents the data type of the variable, which can be integer, floating-point, character, pointer, etc., or a user-defined object.
variable_list can consist of one or more variable names, separated by commas; a variable is composed of letters, digits, and underscores and begins with a letter or underscore.
Here are several valid declarations:
Define an integer variable:
int age;
In the code above, age is defined as an integer variable.
Define a floating-point variable:
float salary;
In the code above, salary is defined as a floating-point variable.
Define a character variable:
char grade;
In the code above, grade is defined as a character variable.
Define a pointer variable:
int *ptr;
In the code above, ptr is defined as an integer pointer variable.
Define multiple variables:
int i, j, k;
int i, j, k; declares and defines the variables i, j, and k, which instructs the compiler to create variables of type int named i, j, k .
Variable initialization
In C, variable initialization means assigning an initial value at the same time the variable is defined. Initialization can be done at definition time or in later code.
An initializer consists of an equals sign followed by a constant expression, as shown below:
type variable_name = value;
Wheretype represents the data type of the variable,variable_name is the name of the variable,value is the initial value of the variable.
A few examples are listed below:
int x = 10; // 整型变量 x 初始化为 10
float pi = 3.14; // 浮点型变量 pi 初始化为 3.14
char ch = 'A'; // 字符型变量 ch 初始化为字符 'A'
int d = 3, f = 5; // 定义并初始化 d 和 f
byte z = 22; // 定义并初始化 z
// Declare external variables
extern int d;
extern int f;
** Initializing variables later:**
In code after the variable definition, you can use the assignment operator = to give the variable a new value.
type variable_name; // 变量定义
variable_name = new_value; // 变量初始化
The example is as follows:
int x; // 整型变量x定义
x = 20; // 变量x初始化为20
float pi; // 浮点型变量pi定义
pi = 3.14159; // 变量pi初始化为3.14159
char ch; // 字符型变量ch定义
ch = 'B'; // 变量ch初始化为字符'B'
It should be noted that a variable should be initialized before use. The value of an uninitialized variable is undefined and may contain arbitrary garbage. Therefore, to avoid undefined behavior and errors, it is recommended to initialize variables before using them.
Variables without initialization
In C, if a variable is not explicitly initialized, its default value depends on the variable's type and its scope.
For global variables and static variables (static variables defined inside functions and global variables defined outside functions), the default initial value is zero.
The following are the default values of different types of variables when there is no explicit initialization:
- Integer variables (int, short, long, etc.): default value is 0.
- Floating-point variables (float, double, etc.): default value is 0.0.
- Character variables (char): default value is '\0', the null character.
- Pointer variables: default value is NULL, meaning the pointer does not point to any valid memory address.
- Variables of compound types such as arrays, structures, and unions: their elements or members are default-initialized according to the corresponding rules, which may include recursively applying default rules to elements.
It should be noted that local variables (non-static variables defined inside functions) are not automatically initialized to default values; their initial values are undefined (containing garbage). Therefore, before using a local variable, you should explicitly assign it an initial value.
In summary, the default value of a variable in C depends on its type and scope. The default value of global and static variables is 0, that of character variables is \0, that of pointer variables is NULL, while local variables have no default value and their initial values are undefined.
Variable declaration in C
A variable declaration guarantees to the compiler that a variable exists with the specified type and name, so the compiler can continue compiling without knowing all the details of the variable. A variable declaration has meaning only at compile time; at link time the compiler needs an actual variable definition.
There are two cases of variable declaration:
- 1. One case requires storage space to be created. For example, int a already has storage space created at the time of declaration.
- 2. The other case does not require storage space; the variable name is declared using the extern keyword without defining it. For example, extern int a, where variable a can be defined in another file.
Unless the extern keyword is present, it is always a variable definition.
extern int i; //声明,不是定义
int i; //声明,也是定义
For more information about extern, see:C extern keyword.
Example
Try the following example, in which the variables are declared at the top, but defined and initialized inside the main function:
Example
#include
<
stdio.h
// Define variables x and y outside the function
int
x
;
int
y
;
int
addtwonum
(
)
{
// Declare variables x and y as external variables inside the function
extern
int
x
;
extern
int
y
;
// Assign values to the external (global) variables x and y
x
=
1
;
y
=
2
;
return
x
y
;
}
int
main
(
)
{
int
result
;
// Call the function addtwonum
result
=
addtwonum
(
)
;
printf
(
"
result is: %d
"
,
result
)
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
result 为: 3
If you need to reference a variable defined in another source file from one source file, you only need to add a declaration with the extern keyword for that variable in the referencing file.
Code in file addtwonum.c:
#include
<
stdio.h
/*External variable declaration
*/
extern
int
x
;
extern
int
y
;
int
addtwonum
(
)
{
return
x
y
;
}
Code in file test.c:
#include
<
stdio.h
/*Define two global variables
*/
int
x
=
1
;
int
y
=
2
;
int
addtwonum
(
)
;
int
main
(
void
)
{
int
result
;
result
=
addtwonum
(
)
;
printf
(
"
result is: %d
\
n
"
,
result
)
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
$ gcc addtwonum.c test.c -o main
$ ./main
result 为: 3
Lvalues and Rvalues in C
There are two types of expressions in C:
- **Lvalue:** An expression that refers to a memory location is called an lvalue expression. An lvalue may appear on either the left or the right side of an assignment operator.
- **Rvalue:** The term rvalue refers to a data value stored at some address in memory. An rvalue is an expression to which a value cannot be assigned—that is, an rvalue may appear on the right side of an assignment operator but not on the left.
Variables are lvalues, so they can appear on the left side of an assignment. Numeric literals are rvalues, so they cannot be assigned to and cannot appear on the left side of an assignment. The following is a valid statement:
int g = 20;
But the following is not a valid statement and will produce a compile-time error:
10 = 20;

