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

Value of variable can be change 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) of variable and layout of the variable’s memory.It also determine the range of values that can be stored within that memory and the set of operations that can be applied to the variable.

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

variable-copy

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

Syntax of variable

Data_Type  Variable_Name;

E.g.,
Data_Type V1,V2,V3 ; V1 V2 and V3 are three variable of same data type.
In above example, Data type should be a 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 compiler error.

Important points regarding the variable

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

It is simple program to display the value of a, but what will be 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 we have got error message, so you should declare a variable first before its use.

Declaration of variable in c

A variable declaration only provides sureness to the compiler at the compile time that variable exist with the given type and name, so that compiler proceed 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 these variable may be defined within the function or outside of the function “.

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

Definition of variable in c

Definition is, action to allocate storage to the variable in other word we can say variable definition is way to say the compiler where and how much to create the storage for the variable generally definition and declaration occur at 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.

Naming Rule of variable in c

Variable name can be composed with letters, digits or underscore. C is a case sensitive language so upper and lower case are totally different to 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 digit, if you have used digit between the identifier then there is no problem.

      int 2length; (wrong)

      int le2ngth; (ok)

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

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

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

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

  • Name of variable can’t be exactly same as of name of function within the scope of the function.

OutPut:  compiler error.

  • Variable name cannot be exactly same as macro name which have declared in header file of source file.

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