variable in c language

variable in c language

A variable in C defines a location name where you can put value and you can use these value whenever required in the program. In other words, you can say that variable is a name (or identifier) which indicate some physical address in the memory, where data store in the form of bits of the string.

Value of a variable can be changed at different times of executions and it may be chosen by the programmer in a meaningful way.

Every variable in C has a specific data type, which determines the size (Generally in Byte) and layout of the variable’s memory. It also determines the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

Basically, a variable used to store some form of data. It can be store single data, group of data (array) or a combination of different type of data (structure, union, enum).


Note: Each variable in c bind with two important properties, scope, and extent. Here I will not discuss the extent and scope

Syntax of variable:

Data_Type  Variable_Name;

For example,

Data_Type V1, V2, V3;  // V1 V2 and V3 are three variables of the same data type. In the above example, data type should be valid. It can be int, char, float, etc or any user-defined data type like structure, union or enum etc.


Note: Variable name should be unique otherwise you will get a compiler error.


Important points regarding the variable in C:


We must declare a variable first before use it, otherwise, we will get a compiler error.
See the below example code.



Above program display the value of a, but what will happen?  When you will compile this program, you will get error message “undefined variable a”.

In above code variable “a” is not declare that’s why you are getting an error message, so you should declare a variable first before its use.

For Example,


Declaration of a variable in C:

A variable declaration only provides sureness to the compiler at the compile time that variable exists with the given type and name, so that compiler proceeds for further compilation without needing all detail of this variable.

“When we declare a variable, then we only give the information of variable to the compiler, but there is no memory reserve for it. It is only a reference, through which we only assure to the compiler that this variable may be defined within the function or outside of the function “.

Note: We can declare a variable multiple times but defined only once.

Using the variable p write down some declaration:

1. An integer variable.
2. An array of five integers.
3. A pointer to an integer.
4. An array of ten pointers to integers.
5. A pointer to a pointer to an integer.
6. A pointer to an array of three integers.
7. A pointer to a function that takes a pointer to a character as an argument and returns an integer.
8. An array of five pointers to functions that take an integer argument and return an integer.


1. int p; // An integer
2. int p[5]; // An array of 5 integers
3. int*p; // A pointer to an integer
4. int*p[10]; // An array of 10 pointers to integers
5. int**p; // A pointer to a pointer to an integer
6. int(*p)[3]; // A pointer to an array of 3 integers
7. int(*p)(char *); // A pointer to a function a that takes an integer
8. int(*p[5])(int); // An array of 5 pointers to functions that take an integer argument and return an integer


If you want to learn more about the c language, here 10 Free days C video course for you.

C tutorial


A variable declaration is useful when you are working on a project which contains multiple files. So if you define a variable in one of the files and it is used in other files so you have to use an extern keyword to declare a variable.

Suppose a project contains two file Display.c and Calculation.c. A variable ‘gData’ which defines in Calculation.c and need to access in Display .c. So you have to declare in Display.c using extern keyword.



Definition of a variable in C:

A definition is an action to allocate storage to the variable in another word we can say that a variable definition is a way to say the compiler where and how much to create the storage for the variable. Generally, definition and declaration occur at the same time but not almost.

Note: When you define a variable then there is no need to declare it but vice versa is not applicable.

Identifier in C:

An identifier is a sequence of digits and non-digit characters(including the underscore _, the
lowercase, and uppercase Latin letters, and other characters), which refers to a name of variables, functions, structures etc. In identifier lowercase and uppercase letters are distinct and there is no specific limit on the maximum length of an identifier.

Naming Rule of the variable in C:

A variable name can be composed of letters, digits or underscore. C is a case-sensitive language so upper and lower case are totally different from each other. Supposed you create a variable “DATA” (upper case) and “data” (lower case) are different.

There are few rules to give a name to the variables in C:

  • There is no space between the variable names.

       int len gth; (wrong)

  • A variable name cannot start with a digit but you use it between the identifier.

      int 2length; (wrong)

      int le2ngth; (ok)

  • You cannot use special character between identifier, but you can use underscore anywhere in the identifier.

       int [email protected]; (wrong)
       int le#ngth ; (wrong)
       int len_gth; (ok)
       int _length; (ok)

  • You can’t use the keyword a variable name, when you will use a keyword as a variable name then you will get a compiler error.

       int if; (wrong)
       char while; (wrong)

  • A variable name can not same as function name within the same scope.

OutPut:  compiler error.

  • A variable name cannot be exactly the same as a macro name which included in the source file.

if macro _TRUE  has already defined in printf.h header file then you can’t use _TRUE as a variable name, if you do that then you will get the compiler error.

Leave a Reply