Variables, Constants and Keywords

Variables

A computer program is executed in RAM (virtual,main memory) and stored in hard disk.Main memory consists of lower to higher memory address.This memory range is very vast.Any location can be used by any program at run time.A reference is created at run time to these locations. 
            Variables are the names assigned to the memory location which is allocated at run time. Variables are used to refer the memory locations and the unit this location holds is called the value of the variable.

Variable declaration conventions

  1. A variable name must not begin with a digit.
  2. A variable name must not contain the special symbols($,#%,^..) except the underscore(_).
  3. Predefined keywords cannot be used as variable name.
  4. A variable name should not contain the spaces in between.

Defining the variables

Variable definition  consist of
  • data type(int ,float ,double...)
  • variable name
  • assignment
  • value
  • semicolon
int a;       //variable declaration
Here int is a datatype and a is a variable name, semicolon indicated that it is the end of the statement ,that is for compiler purpose.
'int a' just declares the variable a, 'a' refers to the memory location lets say address (4000 in hexadecimal) but here a is not defined yet means no value of integer type is assigned to the variable a.'a' refers to the memory location 4000 ,it holds the garbage value.If it would have been used by any program executed earlier it will hold that value else if this is your first program then it holds any value ,you can't predict the value in this case.Now

a=10;        //variable definition
This statement assigns the 10 value to the location 4000.If you want to access the value 10 you just need to use variable a. example: a+10; it gives you 20.

int b=20;      //variable definition and declaration
In this statement ,variable 'b' is a declared and assigned the value 20.

Don't do this with variable names
  1.  3abc                      // digit
  2.  manager salary    //spaces
  3.  while                    //keyword
  4.  $fat                      //special symbol

Constants

A constant is a value that does not change throughout a program. They are of two types:
         1. Primary
         2. Secondary
Primary:
        1. Integer Constants: They contain only integer values.
          int const a =13;
            2. Character Constant: They contain only character values.
                char const c='a';
            3. Float Constants: They contain only float values.
                float const f=1.29;
We will discuss secondary constants later.

Keywords

Keywords are the reserved words.They are predefined in the C library and perform certain tasks.There are 32 keywords in C.All keywords must be written in lower case.They cannot be used as variable names.
  

0 comments:

Post a Comment