variable in c language ?.

variable in c language

A variable defines a location name where we can put value and we can use these value whenever required in the program. In another word, we can say that variable is a name (or identifier) which indicate some physical address in the memory, where data store in form of the bits of 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) of variable 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 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).

variable-copy

Note: Each variable bind with two important properties, scope, and extent. Here I will not discuss 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 the same data type.
In 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

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 we are getting an error message, so you should declare a variable first before its use.

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.

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.

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