The Wayback Machine - https://web.archive.org/web/20250209192207/https://www.geeksforgeeks.org/variables-in-c/
Open In App

C Variables

Last Updated : 21 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

In C, variable is a name given to the memory location that helps us to store some form of data and retrieves it when required. It allows us to refer to memory location without having to memorize the memory address. A variable name can be used in expressions as a substitute in place of the value it stores.

Example:

C
#include <stdio.h>
int main() {

    // Declaring variables
    int a;
  
    // Storing data
    a = 25;

    printf("%d", a);
    return 0;
}

Output
25

Explanation: In the given program, a is a variable that serves as a name for a memory location to store an integer value. It store the value 25 and later retrieve it for printing without needing to reference its full memory address.

Syntax

In C, we have to declare the type of data the variable would store along with the name while creating a variable:

data_type variable_name;

This data_type decides how much memory a variable need. We can choose the data types provided by C language to store different type of data in the variable.

We can also create multiple variables in a single statement by separating them using comma:

data_type v1, v2, v3 ….;

where v1, v2 … are the names for variables.

Example

int var; // integer variable
char a; // character variable
float fff; // float variable

The above syntax is called variable definition. In this, a variable is only named and allocated some memory to it. No data is stored in it till now.

The data is first entered in the variable in the process called variable initialization. It is nothing but assigning some initial value to it using assignment operator (=).

variable_name = value;

It can also be combined with variable definition:

data_type variable_name; = value;

variable declaration breakdown

Variable Syntax Breakdown

Example of Variable

C
#include <stdio.h>

int main() {
  
    // Creating a variable to store age
    // of a student
    int age = 25;
  
    // Updating the age
    age = 18;

    printf("Age: %d\n", age);
      
    // Creating variables to store grades
    // in different subjects
    char math, physics, chemistry, arts, sports;
    math = 'A';
    physics = 'A';
    chemistry = 'B';
    arts = 'C';
    sports = 'C';
    
    printf("---Grades----\n");
    printf("Mathematics: %c\n", math);
    printf("Physics: %c\n", physics);
    printf("Chemistry: %c\n", chemistry);
    printf("Arts: %c\n", arts);
    printf("Sports: %c", sports);
    return 0;
}

Output
Age: 25
---Grades----
Mathematics: A
Physics: A
Chemistry: B
Arts: C
Sports: C

Rules for Naming Variables in C

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

  1. A variable name must only contain alphabets, digits, and underscore.
  2. A variable name must start with an alphabet or an underscore only. It cannot start with a digit.
  3. No white space is allowed within the variable name.
  4. A variable name must not be any reserved word or keyword.
variable names examples

Also, it is recommended to use such a name for a variable that depicts its purpose.

C Variables – FAQs

Can a variable name in C start with a number?

No, variable names in C must begin with a letter or an underscore. Starting with a number is not allowed.

What happens if you use an uninitialized variable in C?

Using an uninitialized variable in C leads to undefined behaviour, as it contains garbage data until explicitly assigned a value.

Can i store different type of data in different type of variable?

No, you cannot store different type of data in a variable of different type.

Is it possible to change the type of a variable after it is declared?

No, once a variable is declared with a type in C, its type cannot be changed.



Next Article

Similar Reads

three90RightbarBannerImg