Computersnyou

C Programming– From The Very Scratch

Posted on  1/14/2012

Variables

A variable is a container to store data.A variable with different data types, like(int,float,double,char,void),can be used to store different types of data.For instance,an integer variable will store integer number,a floating variable can store real number,a char variable can be used to store character,etc.
In C programming language, a variable can be named only by alphabets(a-z OR A-Z),digits(0-9),and underscore( _ ).A variable name can never start with digits(0-9) but can start with either alphabets(a-z OR A-Z) or underscore( _ ).A variable is case sensitive ,i.e. two variables with same name and spelling but different capitalization are different.Two variables declaration or definition with same name,spelling and capitalization within same scope is not possible but within different scope is possible.A variable cannot be named as the data-types.A variable name can be anything after satisfying the above rules.

Illustration with Examples

1) A variable can be named only by alphabets(a-z OR A-Z),digits(0-9),and underscore(_).

Valid                                                                           Invalid

 

sum                                                                                                                                              $um
ate                                                                                                                                                @te

2) A variable name can never start with digits(0-9) but can start with either alphabets (a-z OR A-Z) or underscore ( _ ).

Valid                                                                            Invalid

 

Out                                                                                                                                               0ut
sum                                                                                                                                              5um
_1_                                                                                                                                               1__
sum1                                                                                                                                            1sum

3) A variable is case sensitive ,i.e. two variables with same name and spelling but different capitalization are different.

Same ( Invalid )                                                 Different ( Valid )

 

sum sum                                                                                                                                      Sum sum
Sum Sum                                                                                                                                      sum Sum
SUm SUm                                                                                                                                     SUm Sum
SUM SUM                                                                                                                                      SUM SUm
a a                                                                                                                                                   a A

4) Two variables declaration or definition with same name,spelling and capitalization within same scope is not possible but within different scope is possible.

Invalid [ Within Same Scope ] ( Compilation Error )

 

Screenshot-3Screenshot-4

 

Valid [ Within Different Scope ] ( No Compilation Error )

 

Screenshot-7
Screenshot-8

5) A variable cannot be named as the datatypes (like int,float,char,double,void).

Invalid ( Compilation Error )

 

Screenshot-9
Screenshot-10

 

Valid ( No Compilation Error )

 

Screenshot-11
Screenshot-12

You can write the same codes in your own suitable C compilers.Compile it and then run it.It will work same in all compiler whether it is the Windows C compilers or Linux GCC complier.
So,now I hope that you will not make blunders while declaring and defining your variables.
If you have any problem in any part just leave your comments.I will try to come up with proper explanation as early as possible.
Thanks !!!


  • Home
  • About