Skip to content Skip to main navigation Skip to footer

C Variables

c variables

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

Variable Name

Variable names are identifiers in C programs and have strict naming rules.

  • Can only be composed of letters (both uppercase and lowercase), digits, and underscores (_).
  • 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 in each line are invalid.

Variable names are case-sensitive; star、Star、STAR are all different variables.

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

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 that start with two underscores and an underscore + uppercase are reserved by the system and you should not create such variable names yourself.

Variable Definition in C

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

Each variable has its own type . When declaring a variable, you must tell the compiler the type of the variable.

int height;

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

If several variables have the same type, they can be declared in the same line.

int height, width;
//equals to

int height;
int width;

Note: that statements that declare variables must end with a semicolon.

Once a variable has been declared, the type of that variable cannot be changed at runtime.

Variable Assignment in C

The C programming language allocates memory space for variables when they are declared, but does not clear the original value from memory. This results in the variable being a random value after it is declared. Therefore, before a variable can be used, a value must be assigned to it.

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

int num;
num = 42;

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

The value of a variable should be the same as its type and should not be assigned a value that is not of the same type. For example, if the variable num is an integer, the variable should not be assigned a decimal value. Although C automatically converts types, you should avoid inconsistent types on both sides of the assignment operator.

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

int num = 42;

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

int x = 1, y = 2;

Note: that the return value of the assignment expression is 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 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 valid code that assigns values to multiple variables at once. The assignment operator is executed from right to left, so n is assigned first, and then m, z, y and x are assigned in that order.

Lvalues and Rvalues in C

The C programming language has the concept of left and right values.

A left value is a value that can be placed to the left of an assignment operator, usually a variable; a right value is a value that can be placed to the right of an assignment operator, usually a value. This is to emphasize that some values cannot be placed to the left of the assignment operator, e.g., x = 1 is a legal expression, but 1 = x would be reported as an error.

Variable Scope in C

There are two main types of variable scopes in C: file scope and block scope.

File scopes are variables declared at the top level of the source code file and are valid from the location of the 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 level of the file, and the entire current file from the declaration position is its scope. This variable can be read anywhere within this scope, for example, inside the function main().

A block scope is a block of code consisting of curly brackets ({}) that forms a separate scope. Any variable declared within the block scope is only valid within 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, this variable does not exist.

Code blocks can be nested, i.e., there are code blocks inside code blocks, which creates multiple layers of block scopes. Its 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 variable has the same name as the outer layer, then it will overwrite the outer layer 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. Each scope takes precedence over the i declared by the current scope.

The most common block scope is a function, where variables declared inside the function are not visible outside the function. for loop is also a block scope, where loop variables are only visible inside the loop body and 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 it is still a block scope, and reading the loop variable i from outside will cause the compiler to report an error.

Was This Article Helpful?

1
Related Articles
0 Comments

There are no comments yet

Leave a comment

Your email address will not be published.