C language – Variables in C Programming language

Variables in C language

Each variable in the C language has a particular type, which determines the size and layout of the variable’s memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.


The name of a variable can be composed of letters, digits, and the underscore character. It has to start with either a letter or an underscore. Upper and lowercase letters are distinct due to the fact, that the C language is case-sensitive.

variables in C in language

Rules for naming variables in c language:

  1. A variable name can be any combination of 1 to 8 alphabets, digits, or underscore.
  2. First character must be an alphabet or an underscore(_).
  3. The length of the variable cannot exceed 8 characters long, and some of the ‘C’ compilers can be recognized as up to 31 characters long.
  4. No commas or blank spaces are allowed within a variable name and no special symbol, an underscore can be used in a variable name.

[Note: Use the meaningful and intelligent variable names]

Variable declaration in C

A variable declaration provides assurance to the compiler that there’s one variable being with the given type and name so that compiler proceeds with further compilation without needing complete detail about the variable.

Variable declaration has its aim at the time of compilation only, the compiler needs an actual variable declaration at the time of linking of the program.

And the variable declaration is helpful when you’re using multiple files and you specify your variable in one of the files, which will be available at the time of linking the program.

You’ll use the extern keyword to declare a variable at any place. Though you can declare variables multiple times in your C language program they can be defined only once in a file, a function, or a block of code.

After designing suitable variable names, you must declare them in a program, and this declaration tells the compiler what the variable name and type of the data that the variable will hold.

Syntax:  data_v1, v2, v3,…vn;


data_type       is the type of the data(int, char, float, or other)

Example:  int code;

                  char gender;

                  float price;

                 char name[10];

Here the variable, code is a type of integer, gender is a type of character, price is a type of float, and name[10] is defined id defined array of characters.

Then data items must be assigned to the variable at some part of the program. The data item can then be accessed later in the program simply by referring to the variable name

variable declaration in c programming language

Variable initializing in C

Initialization of variables can be done using the assignment operator ( = ). The variables can be initialized while the declaration itself.

Syntax :   variable = constant;


i, f,c are the type of int, float, char data types

Example: int i = 29;

                 float f = 29.78;

                 char c = ‘l’;

variables in C programming language

Try the following example, where variables have been declared, and after initializing the value of variables.

variable in c programming language

When the above code is compiled and executed, it produces the following result in C Programming language:

value of c: 30
value of f: 23.333334

The same concept applies on function declaration where you provide a function name at the time of its declaration and its actual definition can be given anywhere else. For example:

variables in c programming language

Typedef and Enumeration constants in c language

Typedef declaration:

C language provides a feature to declare a variable of the type of user-defined type declaration.

This allows users to define an identifier that would represent an existing data type and this can later be used to declare variables.

The typedef is a keyword in the C programming language. The purpose of typedef is to form complex types from more basic machine types and assign simpler names to such combinations.

They are most often used when a standard declaration is cumbersome, potentially confusing, or likely to vary from one implementation to another.

Syntax:   typedef data_type identifier;


typedef    is the user-defined type declaration.

data_type    is the data type.

the identifier is the identifier that refers to the new name given to the data type.


       typedef int marks;  

       marks m1, m2, m3;

Here marks are the type of the int and this can be later used to declare variables.

Typedef variable in c language

Enumerated data types:

The C language provides another user-defined data type called enumerated data type.

Syntax: enum identifier { value1, value2, ……,value n};


identifier  is the user defined enumerated data type.

value 1, value 2, …… value n  are the enumeration constants.

enumeration variable in C language

In C programming language, an enumerated data type consists of a set of named values called elements, members, or enumerated of the type.

The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value. the identifier follows with the keyword enum id used to declare the variable, which can have only one value from the enumeration constants.

[ Note: The compiler automatically assigns integer values from 0 to all the enumeration constants.]

Scope of Variables in C language

The scope of a variable implies the availability of variables within the program. Variables have two types of scopes described below.

 Local Variables

Variables that are defined inside a function block or inside a compound statement of a function subprogram are called local variables.


int i , j ;
/* body of the function */

The integer variables i and j are defined inside the function block of function (). Hence i, j  are called local variables i.e., the availability of the variables i, j are local to that function only in which they are declared.

Global/ External Variables in C programming language

The variables that are declared before the function main() are called the global/external variables, These are available for the functions inside the program.


int a, b = 2 ;
main ()
. . . . .
fun () ;

The integer variables a and b are global/external variables, as they are declared before the function main(). These global/external variables are later used in the function sub-program fun().