VOOZH about

URL: https://www.geeksforgeeks.org/c/variables-in-c/

⇱ Variables in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Variables in C

Last Updated : 9 Apr, 2026

A variable in C is a named piece of memory which is used to store data and access it whenever required.

  • It allows us to use the memory without having to memorize the exact memory address.
  • To create a variable in C, we have to specify a name and the type of data it is going to store.
  • C provides different data types that can store almost all kinds of data. For example, int, char, float, double, etc.
  • Every variable must be declared before it is used. We can also declare multiple variables of same data type in a single statement by separating them using comma .

Output
Age: 20
Height: 5.7
Grade: A

Rules for Naming Variables in C

We can assign any name to a C variable as long as it follows the following rules:

  • A variable name must only contain letters, digits, and underscores.
  • It must start with an alphabet or an underscore only. It cannot start with a digit.
  • No white space is allowed within the variable name.
  • A variable name must not be any reserved word or keyword.
  • The name must be unique in the program.

C Variable Initialization

  • Once the variable is declared, we can store useful values in it. The first value we store is called initial value and the process is called Initialization. It is done using assignment operator (=).
  • It is important to initialize a variable because a C variable only contains garbage value when it is declared. We can also initialize a variable along with declaration.

Output
Age: 20
Height: 5.7
Grade: A

Note: Values should be type-compatible, otherwise C performs implicit or explicit type conversion.

Accessing Variables

The data stored inside a C variable can be easily accessed by using the variable's name.


Output
3

Changing Stored Values

We can also update the value of a variable with a new value whenever needed by using the assignment operator =.


Output
Initial value: 10
Updated value: 25
After adding 5: 30

How to use variables in C?

Variables act as name for memory locations that stores some value. It is valid to use the variable wherever it is valid to use its value. It means that a variable name can be used anywhere as a substitute in place of the value it stores.


Output
60
70

Memory Allocation of C Variables

When a variable is declared, the compiler is told that the variable with the given name and type exists in the program. But no memory is allocated to it yet. Memory is allocated when the variable is defined.

Most programming languages like C generally declare and define a variable in the single step. For example, in the above part where we create a variable, variable is declared and defined in a single statement.

The size of memory assigned for variables depends on the type of variable. We can check the size of the variables using sizeof operator.


Output
4 bytes

Variables are also stored in different parts of the memory based on their storage classes.

Recommended Articles

Comment