Wednesday, June 24, 2015

C Variables

In the previous post I described you about the Types of C constant.Now moving on to variables.
Variables are the identity  which can only hold a particular type of data. For Example: an Integer type variable can hold only integer type of value or constant,character variable  can only hold character constant and same with other types also.

Rules for constructing any type of variable:
1)-Any variable name can be formed with the help of alphabets,digits and underscore.Do not create unnecessarily long names as it will create a problem later as it adds to your typing effort when you needs to use a variable again and again.

2)-No commas or blanks are allowed.

3)-No special Character other than underscore are allowed in variable name.

4)-The first letter or character of the variable name should start with alphabet.

examples: nxt_35_e
                 wan_pro
                 amt_per

Now the important point which differentiate these variables in C are there data types.
So to declare any variable in C we must have to follow the format as

                               (Data type) (variable name);

                           eg: int nxt_35_e;
Note:- there should be a gap/space in between the Data type and Variable name.
 For good programming it is advisable to use the small length variable names.

Keywords:

Keywords are the reserved words which are predefined in the C compiler. They cannot be used as variable names. There are only 32 Keywords available in C these are listed below:

1)-auto: It Defines a local variable as having a local lifetime.
              syntax:
                          [auto] data definition;

2)-break: It passes the control out of the compound statement block.      
                syntax:
                            break;

3)-switch
4)-case
5)-default
           :these three are the part of the branch control statements
           syntax:
                      for switch:
                                        switch(expression) statement
                      for case:  
                                         case constant expression:
                      for default:
                                         default:

6)-char : character data type.
7)-int :   integer data type.
8)-const: Makes the variable value or pointer parameter unmodifiable.
               syntax:
                           const variable_name=value;
9)-continue: It passes the control to the beginning of the loop.
                   syntax:
                              continue;
10)-do: it is used in do while loop
           syntax:
                       do
                       {
                         //statements
                         }while(expression);
11)-enum: Defines the set of constants of type int.
                syntax:
                            enum [tag] {name [=value], ..... }
12)- extern: It indicates that an identifier is defined elsewhere.It may be applied to data definitions                         and function prototype             
                    syntax:
                               extern data_definition;
                               extern function-prototype;

13)-float:
                    ----------------------------Floating point data types.
14)-double:

15)-for:      for loop
                   syntax:
                               for(initialization;condition;update)
                               {
                                  //statements;
                                  }

16)-goto: unconditional transfer control
                syntax:
                           goto identifier;

17)-if
              ---------------------------------------Conditional statement
18)-else
              :syntax:
                          if(expression)
                          statement 1;
                          else
                           statement 2;

19)-register: It tells the compiler to store the variable which is being declared in the CPU register.
                    syntax:
                               register data-definition;

20)-return: Exits the function.
                  syntax:
                             return(expression);

Type Modifiers:21 to 24
21)-short
22)-long
23)-signed
24)-unsigned

25)-sizeof: It returns the size of expression or type
                 syntax:
                             sizeof(expression);
                             sizeof(type);

26)-static: It preserves variable value to survive after it's scope ends.It is applied to both data-                               definition or function definition.
                  syntax:
                             static data_definition;
                             static function-definition;

27)-struct: It groups different data type variables into a single record.

28)-typedef: It creates a new type.
                    syntax:
                               typedef type-definition identifier;

29)-union: It groups the variable which shares the same storage space
                 syntax:
                             union [union-type-name]
                             {
                               type variable-names;
                                .  .   .   .
                               } [union-variables];

30)-void: It's an Empty data type.

31)-volatile: It indicates that a variable can be changed by it's background routine
                   syntax:
                               volatile data-definition;

32)-while: while loop ,it repeats execution if while condition is true
                 syntax:
                             initialization;
                             while(condition)
                             { 
                                //statements;
                                update;
                              }

Note that some vendors like Microsoft design there own compiler with some extra keywords apart from the ones mentioned above.