Variables in C

Variables in C

Variable is an identifier used to name the memory location which holds the value. Variable is an entity whose value can be changed (Not Fixed) during the program execution.

Need for variables

It may help to think of variables as a placeholder for a value or data. You can think of a variable as being equivalent to its assigned value or data. In a C program, if a user wants to store and use any data then the user must create variables in a program to store the required data.

Rules for naming a Variable

1. The variables must always begin with a letter or underscore as the first character.

2. The following letters can be any number of letters, digits or underscore.

3. Maximum length of any identifier is 31 characters for external names or 63 characters for local names.

4. Identifiers are case sensitive. Ex. Rate and RaTe are two different identifiers.

5. The variable name should not be a keyword.

6. No special symbols are allowed except underscore.

Declaring a Variable

A variable thus has three attributes that are of interest to us: its type, its value and its address and before a C program can utilize memory to store a value or data it must claim the memory needed to store the values for a variable. This is done by declaring variables.

Declaring variables is the way in which a C program shows the number of variables it needs, name of the variables, range of values it can represent and how much memory they need. Within the C programming language, when managing and working with variables, it is important to know the type of variables and the size of these types. Size of the datatypes can be hardware specific – that is, how the language is made to work on one type of machine can be different from how it is made to work on another.

All variables in C are typed. That is, every variable declared must be assigned with certain datatype.

General Syntax for declaring variables:

DataType Variable-list;

Variables can be declared as

1) Local variables: When variables are declared inside functions as follows they are termed local variables and are visible (or accessible) within the function ( or code block ) only.

For Example :

int main()
int i, j;

In the above example, the variables i and j are local variables, they are created i.e. allocated memory storage upon entry into the code block main() and are destroyed i.e. its memory is released on exit from the block. Therefore i and j are local to main().

2) Global Variables: When variables declared outside functions they are termed as global variables and are visible throughout the program. These variables are created at program start-up and can be used for the entire lifetime of the program.

For Example,

int i;
int main()
{ ... }

In this example, i is declared as global variable and it is visible throughout the program.

Initialization or Assignment of a Variable

When variables are declared in a program it just means that an appropriate amount of memory is allocated to them for their exclusive use. This memory however is not initialized to zero or to any other value automatically and so will contain random values unless specifically initialized before use. Hence, variables may need to be assigned and store value in it. It is done using initialization.

General Syntax for initialization :-

Initialization of variable is done using assignment operator(=).

variable-name = expression ;


Expression can be - arithemetic expression, relational expression, logical expression, conditional expression, constant or an identifier that must reduce to a single value.

For Example :-

1) char ch;

ch = 'a' ; /* in this example, a character constant „a‟ is assigned to variabla ch */

2) double d;

d = 12.2323 ; /* in this example, a floating point constant is initialized to d */

3) int i, j,k;

i = 20 ;


k=i+j; /* in this example, i is initialized to 20, j is initialized to 100 and k is initialized to addition of i and j i.e. 120 */

C : Variables in C Program List

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Website development Company in Allahabad