C Variables

Summary: in this tutorial, you will learn about C variables that allow you to manipulate data in your program.

C Variables

What is a variable?

A variable is a meaningful name of data storage location in computer memory. When using a variable, you refer to a memory address of computer.

Naming variables

Each variable has a name, which is called variable name. The name of variable is also referred as identifier. In C, the name of a variable must follow these rules:

  • The variable name can contain letters, digits and the underscore ( _) . The first character of the variable name must be a letter or underscore ( _). However you should avoid using underscore ( _) as the first letter because the it can be clashed with standard system variables.
  • The length of the variable name can be up to 247 characters long in Visual C++ but 31 characters are usually adequate.
  • Variable name must not be the same as C reserved words or keywords.

The following table illustrates the keywords in C:

autobreakintreturn
case charregistersigned
constcontinueshortstatic
defaultdosizeofswitch
doubleelsestructunion
enumexterntypedefvoid
floatforunsignedwhile
gotoifvolatile

C variables naming conventions

The following are general conventions for naming variables:

  • Use descriptive variable name.
  • Begins with a lowercase letter.
  • Separate words within the variable name with underscore ( _) e.g., min_height or mixed lower case and upper case e.g.,  minHeight.

Notice that C variable is case-sensitive, so the min_height and Min_height are different variables.

C variables declaration

Before using a variable, you must declare it. To declare a variable, you specify its data type and its name. The variable declaration statement always ends with a semicolon ( ;). For example:

If multiple variables share the same data type, you can declare them in a single statement as follows:

When you declare a variable:

  • C reserves a space in the memory to hold the value of the variable. The amount of memory depends on the data type associated with the variable.
  • C also allocates spaces that associate with the variable name and a unique address.

A variable can be declared at any point in the program before it is used. It is good practice to declare a variable closest to its first point of use. In C, declaring a variable is also means defining it.

C variables initialization

To make it more convenient, C allows you to initialize a variable when you declare it, e.g.:

It is good practice to place initialized variables on a separate line and add a descriptive comment if possible to explain why the variable is initialized to a specific value. For example:

Don’t do this:

C variables assignments

To assign a value to a variable, you can either initialize it through the declaration or use the assignment operator ( =) to assign it a value. See the following example:

First, we declared a variable, x, and initialize its value to 10. Next, we assigned 20 to x. Then, we assigned x to y, so the value of y is 20.

Notice that the assignment operator (=) means assign the value of the right-hand side to the variable on the left-hand side. It does not mean equality.

register variables

It is faster to access the registers than the main memory. Therefore if you have a variable that is most frequently used and it is required fast access, you can put the variable in registers using the register keyword. For example:

Notice that the register keyword works like a directive i.e., it does not guarantee the allocation of a register for storing value of the variable. It is the compiler’s choice.

extern variables

When you develop program in C, you typically organize the code into header files with .h extension and source code files with .c extension.

In a header file, you may have the following line of code:

Because we used the extern modifier, we are telling the compiler that there is an counter integer variable defined somewhere else so that the compiler does not allocate memory for the counter variable.  This statement only declares the counter variable.

Whenever the compiler sees the following code in a source code file:

It will allocate memory for the counter variable. This statement defines the counter variable.

In this tutorial, we have introduced you to the concept of C variables including declaring, initializing and assigning variables.