Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views5 pages

C Variables - GeeksforGeeks

The document provides an overview of variables in C programming, explaining their purpose as names for memory locations to store and access data. It covers how to create, initialize, and access variables, as well as the rules for naming them and their memory allocation. Additionally, it discusses variable scope and the concept of constants, which are immutable variables defined with the 'const' keyword.

Uploaded by

amolpatil30990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

C Variables - GeeksforGeeks

The document provides an overview of variables in C programming, explaining their purpose as names for memory locations to store and access data. It covers how to create, initialize, and access variables, as well as the rules for naming them and their memory allocation. Additionally, it discusses variable scope and the concept of constants, which are immutable variables defined with the 'const' keyword.

Uploaded by

amolpatil30990
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4/23/25, 11:24 AM C Variables | GeeksforGeeks

Search...

C Variables
Last Updated : 07 Apr, 2025

In C, variable is a name given to the memory location to easily store


data and access it when required. It allows us to use the memory
without having to memorize the exact memory address. A variable
name can be used anywhere as a substitute in place of the value it
stores.

Create Variables
To create a variable in C, we have to specify its name and the type of
data it is going to store. This is called variable declaration.

data_type name;

We can also create multiple variables of same in a single statement by


separating them using comma:

data_type name1, name2, name3, ...;

C provides a set of different data type that are able to store almost all
kind of data. For example, integers are stored as int type, decimal
numbers are stored by float and double data types, characters are
stored as char data type.

Rules for Naming Variables

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


following rules:

A variable name must only contain alphabets, digits, and


underscore.
A variable name must start with an alphabet or an underscore only.
It cannot start with a digit.

https://www.geeksforgeeks.org/variables-in-c/ 1/10
4/23/25, 11:24 AM C Variables | GeeksforGeeks

No white space is allowed within the variable name.


A variable name must not be any reserved word or keyword.

Initialization
No useful data is stored inside a variable that is just declared. We have
to initialize it to store some meaningful value to the variable. It is done
using assignment operator =.

int n;
n = 3;

We can also initialize a variable with declaration.

int n = 3;

Note: It is compulsory that the values assigned to the variables


should be of the same data type as specified in the declaration.

Accessing
The data stored inside a variable can be easily accessed by using the
variable’s name. For example:

▸ {...}
// Access the value stored in
// variable
printf("%d", n);

▸ {...}

Output

C C Basics C Data Types C Operators C Input and Output C Control Flow C Functions C Arr
Updating
We can also update the value of a variable with new value whenever
needed by using the assignment operator =.

Example:

https://www.geeksforgeeks.org/variables-in-c/ 2/10
4/23/25, 11:24 AM C Variables | GeeksforGeeks

#include <stdio.h>

int main() {

// Create integer variable


int n = 3;

// Change the stored data


n = 22;

// Access the value stored in


// variable
printf("%d", n);
return 0;
}

Output

22

How to use variable?


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. For
example, an integer variable can be used in a mathematical expression
in place of numeric values.

#include <stdio.h>

int main() {

// Expression that uses values


int sum1 = 20 + 40;

// Defining variables
int a = 20, b = 40;

// Expression that uses variables


int sum2 = a + b;

printf("%d\n%d", sum1, sum2);


return 0;
}

Output

60
60

https://www.geeksforgeeks.org/variables-in-c/ 3/10
4/23/25, 11:24 AM C Variables | GeeksforGeeks

Memory Allocation of 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.

Example:

▸ {...}
int num = 22;

// Finding size of num


printf("%d bytes", sizeof(22));

▸ {...}

Output

4 bytes

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

Scope of Variable
We have told that a variable can be accessed anywhere once it is
declared, but it is partially true. A variable can be accessed using its
name anywhere in a specific region of the program called its scope. It is
the region of the program where the name assigned to the variable is
valid.

A scope is generally the area inside the {} curly braces.

Example:

// num cannot be accessed here

int main() {

https://www.geeksforgeeks.org/variables-in-c/ 4/10
4/23/25, 11:24 AM C Variables | GeeksforGeeks
// num cannot be accessed here
{
// Variable declaration
int num;
}

// Cannot be accessed here either


return 0;
}

Constants
We know that we can change the value stored by any variable anytime,
but C also provides some variables whose value cannot be changed.
These variables are called constants and are created simply by
prefixing const keyword in variable declaration.

Syntax:

const data_type name = value;

Constants must be initialized at the time of declaration.

Comment More info


Next Article
Placement Training Program Constants in C

Similar Reads

https://www.geeksforgeeks.org/variables-in-c/ 5/10

You might also like