A variable can be understood as the name of a memory location. By using the variable name, you can reference this memory and access the stored value. The value might change, which is why it’s called a variable. If the value remains constant, it’s referred to as a constant.

Variable Names

In C, variable names are considered identifiers, and there are strict rules for naming them:

  • They can only contain letters (both uppercase and lowercase), digits, and underscores (_).
  • They cannot begin with a digit.
  • The name must not exceed 63 characters in length.

Here are some examples of invalid variable names:

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

In each case, the variable name violates one or more of the rules.

Variable names are case-sensitive, so star, Star, and STAR are considered different variables.

Not all words can be used as variable names. Certain words have special meanings in C (e.g., int) or are commands (e.g., continue). These are known as keywords and cannot be used as variable names. Additionally, some words are reserved for future use, known as reserved words, and also cannot be used.

Here 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

Additionally, variable names that start with two underscores or an underscore followed by a capital letter are reserved for the system and should not be used for custom variable names.

Variable Declaration

In C, a variable must be declared before it is used. If a variable is used without being declared, the program will throw an error.

Each variable has a type. When declaring a variable, its type must be specified.

1
int height;

The code above declares a variable height of type int (integer).

If multiple variables share the same type, they can be declared in the same line.

1
2
3
4
5
int height, width;

// Equivalent to:
int height;
int width;

Note that the declaration must end with a semicolon.

Once a variable is declared, its type cannot be changed at runtime.

Variable Assignment

When a variable is declared, memory is allocated for it, but the memory may contain random values. Therefore, a variable should always be assigned a value before use.

Assignment is done using the assignment operator (=).

1
2
int num;
num = 42;

Here, the variable num is declared first, and then assigned the value 42.

The assigned value should match the variable’s type. For example, an integer variable should not be assigned a decimal value. While C automatically converts types in some cases, it is best to avoid mismatching types in assignments.

You can also declare and assign a value to a variable in one line.

1
int num = 42;

Multiple variables of the same type can be assigned values in a single line.

1
int x = 1, y = 2;

An important feature in C is that assignment expressions return a value, which is the value on the right side of the assignment.

1
2
3
4
int x, y;

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

In this example, y is assigned the result of the expression (x = 2 * x), which is 2.

Because assignment expressions return a value, C allows for chained assignments.

1
2
3
int x, y, z, m, n;

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

This code is valid and assigns 3 to all variables in a single statement. The assignments are executed from right to left, starting with n.

C distinguishes between lvalues and rvalues. An lvalue is something that can appear on the left side of an assignment, typically a variable. An rvalue is something that can appear on the right side of an assignment, usually a value. For example, x = 1 is valid, but 1 = x will result in an error.

Variable Scope

Scope refers to the region of the program where a variable is accessible. In C, variables generally have two types of scope: file scope and block scope.

File scope applies to variables declared at the top level of a source file, outside any functions. These variables are accessible throughout the entire file, starting from the point of declaration.

1
2
3
4
5
int x = 1;

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

In this example, the variable x is declared at the file level and can be accessed anywhere within the file, including inside the main() function.

Block scope refers to variables declared within a block of code, defined by curly braces {}. Variables in block scope are only accessible within that block and are invisible outside of it.

1
2
3
4
5
6
7
8
9
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 this example, the variable b is only visible within the if block, and attempting to access it outside the block results in an error.

Blocks can be nested, meaning a block can contain other blocks. Inner blocks can access variables from outer blocks, but not the other way around. If a variable is declared in both an inner and outer block, the inner variable will override the outer one within that inner scope.

1
2
3
4
5
6
7
8
9
10
{
int i = 10;

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

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

Here, the inner block declares a variable i that hides the outer i within its scope.

A common example of block scope is a function. Variables declared inside a function are not accessible outside it. Similarly, the loop variable in a for loop is scoped to the loop itself.

1
2
3
4
5
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}

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

In this example, the loop variable i is inaccessible outside the loop, and attempting to print it will cause an error.