Skip to main content

Variables

A variable can be understood as the name of an area of memory. By means of the variable name, you can refer to this area of memory and get the value stored in it. Since the value may change, it is called a variable, otherwise it is a constant.

Variable names

Variable names are identifiers in C. There are strict rules for naming variables.

  • They can only consist of letters (both upper and lower case), numbers and underscores (_).
  • It cannot start with a number.
  • The length cannot exceed 63 characters.

Here are some examples of invalid variable names.

$zj
j**p
2cat
Hot-tab
tax rate
don't

In the above example, the variable names on each line are invalid.

Variable names are case sensitive, star, Star and STAR are all different variables.

Not all words can be used as variable names. Some words have special meanings in C (e.g. int) and others are commands (e.g. continue), which are called keywords and cannot be used as variable names. In addition, the C language reserves some words for future use, and these reserved words cannot be used as variable names either. The following are the main C keywords and reserved words.

auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, inline, int, long, register, restrict, return , short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while

Also, variable names starting with two underscores and one underscore + uppercase letter are reserved by the system and you should not name them yourself.

Declaration of variables

Variables in C must be declared before they can be used. If a variable is used directly without being declared, an error will be reported.

Each variable has its own type (type). When declaring a variable, you must tell the compiler the variable's type.

int height;

The above code declares the variable height and specifies the type as int (integer).

Several variables can be declared on the same line if they have the same type.

int height, width;

// is equivalent to
int height;
int width;

Note that statements declaring variables must end with a semicolon.

Once declared, the type of the variable cannot be changed at runtime.

Assignment of variables

C allocates memory space for a variable when it is declared, but does not clear the memory of its original value. This results in the variable being declared with a random value. Therefore, a variable must be assigned a value before it can be used.

The assignment operation is done with the assignment operator (=).

int num;
num = 42;

In the example above, the first line declares an integer variable num and the second line assigns a value to this variable.

The value of the variable should be of the same type and should not be assigned a value that is not of the same type, for example num is of type integer and should not be assigned a decimal value. Although C automatically converts types, it is important to avoid inconsistent types on either side of the assignment operator.

The declaration and assignment of variables can also be written on one line.

int num = 42;

Assignments of multiple variables of the same type can be written on the same line.

int x = 1, y = 2;

Note that the assignment expression has a return value equal to the value to the right of the equal sign.

int x, y;

x = 1;
y = (x = 2 * x);

In the above code, the value of the variable y is the return value 2 of the assignment expression (x = 2 * x).

Since assignment expressions have return values, C can be written with multiple assignment expressions.

int x, y, z, m, n;

x = y = z = m = n = 3;

The above code is legal code to assign values to more than one variable at a time. The assignment operator is executed from right to left, so n is assigned first, then m, z, y and x are assigned in that order.

C has the concept of a left value and a right value. A left value is a value that can be placed to the left of an assignment operator, usually a variable, while a right value is a value that can be placed to the right of an assignment operator, usually a concrete value. This is to emphasise that some values cannot be placed to the left of the assignment operator, e.g. x = 1 is a legal expression, but 1 = x will report an error.

Scoping of variables

There are two main types of scope for variables in C: file scope and block scope.

File scope means that variables declared at the top of the source file are valid from the point of declaration to the end of the file.

int x = 1;

int main(void) {
printf("%i\n", x);
}

In the above example, the variable x is declared at the top of the file, and the entire current file from the declaration location is its scope, and can be read anywhere in this scope, for example inside the function main().

Block scope refers to a block of code consisting of curly braces ({}) which forms a separate scope. Any variable declared inside a block scope is only valid for the current block and is not visible outside the block.

int a = 12;

if (a == 12) {
int b = 99;
printf("%d %d\n", a, b); // 12 99
}

printf("%d \n", a); // 12
printf("%d \n", b); // error

In the above example, the variable b is declared inside the if block, so for code outside the curly brackets, the variable does not exist.

Blocks of code can be nested, i.e. there are blocks of code inside blocks of code, which then creates multiple layers of block scope. The rule is that the inner block can use the variables declared in the outer layer, but the outer layer cannot use the variables declared in the inner layer. If the inner layer has a variable with the same name as the outer layer, then it will overwrite the outer variable in the current scope.

{
int i = 10;

{
int i = 20;
printf("%d\n", i); // 20
}

printf("%d\n", i); // 10
}

In the above example, both the inner and outer scopes have a variable i, and each scope will take precedence over the i declared by the current scope.

The most common block scope is the function, where variables declared inside the function are not visible to the outside of the function. A for loop is also a block scope, and loop variables are only visible inside the loop body, not outside.

for (int i = 0; i < 10; i++)
printf("%d\n", i);

printf("%d\n", i); // error

In the above example, the for loop omits the curly braces, but is still a block scope, and reading the loop variable i externally will cause the compiler to report an error.