Saturday, October 19, 2013

const Keyword...


const keyword can be used to define constant of any data type. The const keyword is used as a qualifier to the following data types:
int, float, char ,double and struct. The syntax for declaring variable as constant is as follows:
const data_type constname=constant_value;
Where,
data_type may be valid data type, constname is the name of constant, and constant_value is the value assigned by the user for the defined constant.

 E.g.
const float pi=3.14159;
const int TRUE=1;
const char *bookname=”C philosophy by examples!”
Remember that,
cons tint i; can be declared as
int const i;

Program 18:  
#include <stdio.h>
#include <conio.h>
void main()
{
    const long unsigned hours=24,minutes=60,seconds=60;
    clrscr();
    printf("A day contains total %lu seconds.”,hours*minutes*seconds);
   getch();
}

Output:
#-------------------------------------------------------------------------------#
  A day contains total 86400 seconds.
#-------------------------------------------------------------------------------#

Warning: Don’t try to modify the constant at run time otherwise “cannot modify constant object” error occurs.
What happens when you do not specify the data types during constant declaration? Type the following code:

Program 18:  
#include <stdio.h>
#include <conio.h>
void main()
{
    const PI=3.14159;
    clrscr();
    printf("%f”,PI);
   getch();
}

Now, compile it. No problem at all! Now, run it. Observe the output by pressing ALT+F5. The output would be,

Output:
#-------------------------------------------------------------------------------#
printf: floating point format not linked
abnormal program termination
#-------------------------------------------------------------------------------#

In the line,
const PI=3.14159;
we cannot specify the data type of the constant PI. Always remember that when we do not specify the data type of constant explicitly, by default compiler assumes it as an integer constant. Therefore,

         const PI=3.1415; and
         cons tint PI=3.1415; are similar.

So, the compiler assigns the value 3 to the constant PI and truncates the decimal part, since it is treated as an integer. And in the printf() statement, we specify the format specification as float (%f) instead of integer (%d), so it results in run time error.

The correct version of the above program would be:
#include <stdio.h>
#include <conio.h>
void main()
{
    const float PI=3.14159;
    clrscr();
    printf("%f”,PI);
   getch();
}

Output:
#-------------------------------------------------------------------------------#
 3.141500
#-------------------------------------------------------------------------------#

Changing the value of const: If you want to change the value of const, then you can do so by using the pointer. The following program illustrates this:

Program 19:  
#include <stdio.h>
#include <conio.h>
void main()
{
    int *two_days;
    const long unsigned hours=24,minutes=60,seconds=60;
    clrscr();
    two_days=&hours;
    *two_days=48;
    printf("Two days contains %lu seconds.”,hours*minutes*seconds);
   getch();
}

Output:
#-------------------------------------------------------------------------------#
  Two days contains total 172800 seconds.
#-------------------------------------------------------------------------------#
Here, *two_days is an integer pointer. 

No comments:

Post a Comment