What is Variable in C Programming language in detail

Variable in C language in detail
Variable in C language in detail

Upon Completing this article,you will be able to know:
  • What is Variable in C language?
  • Declaration of Variable in C language.
  • Assigning Value to Variables.
  • Declaring a Variable as Constant.
  • Declaring a Variable as Volatile.

Variables in C:-

A variable is data name that may be used  to store data value. Unlike constant that remain unchanged during the execution of a program.a variable may take different value at different  times during execution.

A variables name can be chosen by the programmer in a meaningful way so as to reflect its function or nature in the program.Some example of such names are :average,height,total,count etc.

Rules for Variable:

  1.   They must begin with a letter.Some system permit underscore as the first character.
  2.   ANSI standard recognizes a length of 31 character. 
  3.   Uppercase and lowercase are significant.
  4.   It should not a keyword,
  5.   White space is not allowed. 

Declaration of Variable in C

There are two types of way to declare Variable in C language:
  1. Primary Type Declaration.
  2. User Defined Type Declaration.

1.Primary Type Declaration:-

A Variable can be used to store a value of any data type.That is,the name has nothing to do with its type.The syntax for declaring variable is as follow;

                                      datatype v1,v2,....vn;

v1,v2,...vn are the names of variable.Variable are separated by commas.A declaration statement must end with a semicolon.For example,valid declaration are:
                       int count;
                       int number,total:
                       double ratio; 
   

2.User-Defined Type Declaration:-

C support a feature know as "type definition" users to define an identifier that would represent an existing datatype.The user-defined data type identifier can later be used to declare a variables.It take general form:

                                    typedef type identifier;

where type refer to an existing data type and "identifier" refer to the "new" name is given to the data type.The existing data type may belong to any class of type,including the user-defined ones.Remember that the new type is 'new' only in name,but not the datatype.typedef cannot create a new type.Some example of type definition are:

                                  typedef int units;
                                  typedef float marks;

The main advantage of typedef is that we can create meaningful datatype names for increasing the readability of the program.

Assigning value to Variable in C:

The Value can be assigned to Variable using the assignment operator(=) as follows:

                                   variable_name=constant;

C permit multiple assignments in one line.For example

                                initial value=0;final value=100;

It is also possible to assign a value to a variable at the time of variable is declared.This takes the following form:

                           datatype variable_name=constant;
S
Some example are:
                             int final_value =100;
                             char yes          ='x';
                             double balance= 75.84;

Declaraing a Variable as Constant in C:

We may like the variable of certain variable to remain constant during the execution of the program.We can achieve this by declaring the variable with qualifier const at the time of initialization.Example:

                                      Const int class size=40;

const is new datatype qualifier defined by ANSI standard.This tells the compiler that the value of the int variable class_size must not be modified by the program.However,it can be used on the right_hand side of an assignment statement like any other variable.


Declaring A  Variable as Volatile in C:

ANSI standard define another qualifier volatine that could be used tell explicitly the compiler that a variable value may be changed at any time by some external source(from outside the program).For example:

                                         volatile int date;

The value of date may be altered by some external factors even if it does not appear on the left hand side of an assignment statement.When we declare a variable as volatile , the compiler will examine the value of the variable each time it is encountered to see whether any external alteration has changed value.


Conclusion:

In this article we have have learnt about what is variable in C,Declaration of Variable in C language,Assigning Value to Variables,Declaring a Variable as Constant and Declaring a Variable as Volatile in detail I hope so you have not any queries related to this topic but if if you any queries then feel free ask in comment section I will try resolve you doubt.
Post a Comment (0)
Previous Post Next Post