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

C Variables

Last Updated : 28 Feb, 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: 18
---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.


Master C with our C Programming online course! From basics to advanced topics like data structures, enhance your skills with hands-on coding challenges. Take the Three 90 Challenge—complete 90% in 90 days, and earn a 90% refund. Start your learning journey today!


Next Article

Similar Reads

Difference between Static variables and Register variables in C
static variables Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope. Syntax: static data_type var_name = var_value; register variables Registers are faster than memory to acces
2 min read
Can Global Variables be dangerous ?
In a small code, we can track values of global variables. But if the code size grows, they make code less understandable (hence less maintainable). It becomes difficult to track which function modified the value and how. C++ // A C++ program to demonstrate that a // global variables make long code less // maintainable. int c = 1; int fun1() { // 10
2 min read
Problem Solving on Storage Classes and Scoping of Variables
Storage class of variables includes the scope, visibility and life-time which help to trace the existence of a particular variable during the runtime of a program. There exist four types of storage classes in C: auto, register, static and extern. Auto and Register Storage Classes - The scope of variable declared using auto or register storage clas
8 min read
Global Variables in C
Prerequisite: Variables in C In a programming language, each variable has a particular scope attached to them. The scope is either local or global. This article will go through global variables, their advantages, and their properties. The Declaration of a global variable is very similar to that of a local variable. The only difference is that the g
3 min read
How will you show memory representation of C variables?
Write a C program to show memory representation of C variables like int, float, pointer, etc. Algorithm: Get the address and size of the variable. Typecast the address to char pointer. Now loop for size of the variable and print the value at the typecasted pointer.Program: c #include <stdio.h> typedef unsigned char *byte_pointer; /*show byte
1 min read
How are variables scoped in C - Static or Dynamic?
In C, variables are always statically (or lexically) scoped i.e., binding of a variable can be determined by program text and is independent of the run-time function call stack. For example, output for the below program is 0, i.e., the value returned by f() is not dependent on who is calling it. f() always returns the value of the global variable
1 min read
What are the default values of static variables in C?
In C, if an object that has static storage duration is not initialized explicitly, then: — if it has pointer type, it is initialized to a NULL pointer; — if it has an arithmetic type, it is initialized to (positive or unsigned) zero; — if it is an aggregate, every member is initialized (recursively) according to these rules; — if it is a union, the
2 min read
Operations on struct variables in C
In C, the only operation that can be applied to struct variables is assignment. Any other operation (e.g. equality check) is not allowed on struct variables. For example, program 1 works without any error and program 2 fails in compilation. Program 1 C #include <stdio.h> struct Point { int x; int y; }; int main() { struct Point p1 = {10, 20};
1 min read
Initialization of variables sized arrays in C
The C99 standard allows variable sized arrays (see this). But, unlike the normal arrays, variable sized arrays cannot be initialized. For example, the following program compiles and runs fine on a C99 compatible compiler. C #include<stdio.h> int main() { int M = 2; int arr[M][M]; int i, j; for (i = 0; i < M; i++) { for (j = 0; j < M; j+
1 min read
Swap Two Variables in One Line
We have discussed different approaches to swap two integers without the temporary variable. How to swap into a single line without using the library function?1) Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write "x, y = y, x".2) C/C++: Below is one generally provided classical solution: /
4 min read