Variables in C

In C, a variable is a name given to a memory location. It is used to store some value that can be used multiple times in the program through the variable.

How a variable is declared?

A variable needs to be declared or created first before using. The basic syntax to declare a variable is –

datatype variable_name;

int a;
float b;

In the above example, we have created two variables named “a” and “b”. The variable a is of type int whereas variable “b” is of type float. This means the variable a can only store the value of integer types like 1, 4, 7, -5, etc and variable b can only store the value of floating-point type like 2.1, 4,5 etc.

We can declare multiple variables in a single statement as long as the variables are of the same type.

For example:

int age, score;
float marks, weight;

In the above example, we have declared two variables of int type in a single statement and two variables of type float in another single statement.

We can name a variable anything we like (an alphabet or a word) unless it is not a reserved keyword. If you don’t know what a reserved keyword is, read this- C Basic syntax.

For example-

int return; // (Wrong)

We cannot declare a variable named ‘return’, as, return is a predefined keyword in C.

Rules of declaring variables

  • A variable can have alphabets, underscore, or digits.
  • A variable name cannot start with a number. Only an alphabet or an underscore is allowed at the start.
  • If a variable name contains multiple words or parts, whitespace should not be there between them.
  • A variable name must not be a reserved keyword.

Some valid variable names

int a;
int age1;
int my_marks;
int _bottles;

Some invalid variable declarations

int return; // (cannot use a keyword as a variable name)
int 1age; // (Cannot start with a number)
int my marks; // (No space is allowed within variable name)

Defining a variable

Defining a variable means storing a value. To store (or to assign) a value in a variable, we use the assignment operator (=). Lets us see.

int age;
age = 14;

In the second statement, we have stored a value 14 in the variable age. In any assigment expression, the value on the right side is assigned to the variable on the left side.

Note: Assignment always happens from right to left.

Assigning value at the time of declaration

We can also assign a value to a variable at the time of declaration. See this example.

int age = 14;

In the above statement, we have assigned a value 14 to the variable ‘age’ at the time of declaration i.e., we have created the variable and stored a value in it at the same time.

We can also do this with multiple variables in a single statement.

For example:

int age = 14, marks = 70;

In the above example, we have declared two variables named ‘age’ and ‘marks’ of int type and assigned them the values 14 and 70 respectively.

Redefining a variable

At any moment, we can change the value of a variable. This is called as redefining the variable.

int x = 11;
printf("The value of x is %d ", x);
x = 14;
printf("Now the value of x is %d ",x);

Types of variables

There are various types of variables in C:

  • Local variables
  • Global variables.
  • Static variables
  • Automatic variables
  • External variables.

We will learn more about variables and their types in the later tutorial.