Monday, October 21, 2013

Using constants


In C any literal; number, single character or character string is known as constant. Constant is the quantity, which does not change during program execution. Consider the following statement:

          z=x+100;

Here, 100 is a constant, and the value 100 cannot be modified during program execution.

Using symbolic constants:
C provides following three ways to define symbolic constants:
    1)   #define preprocessor directive
    2)   enumerated data type
    3)   const keyword
Now, we are going to discuss these constants one by one.

#define preprocessor directive: The C preprocessor is the program, that preprocesses our program before it is passed to the compiler. The preprocessor directive begins with a #symbol. The directive can be placed anywhere in the program, but before using it. The syntax to declare #define constants is as follows:

#define symbolic_name expression

Where,
symbolic_name is the name given to the constant. The rules you applied to naming symbolic constants are same as that for naming variables.
expression can be arithmetic expression, string expression or it may be a character expression.

Examples:
#define MAX 100
#define PI 3.14159
#define MSG “The road to a friend’s house is never long.”

The first example defines the symbolic constant MAX and assigns a value 100 to it. The second example defines the symbolic constant name PI and assigns a value 3.14159 to it. The third example define the symbolic constant MSG and assigns the string “The road to a friend’s house is never long.” to it.

Note: It is not necessary to declare the type of symbolic constant (i.e. int, float or char). It is automatically assigned by the compiler when we define it.
Generally the symbolic constants are written in uppercase letter to distinguish them from the ordinary constants, but this is convention not a rule. If you desire, you can declare them in lower case also. But, it is good practice to make a habit of defining the symbolic constant in upper case letters.
To understand this concept thoroughly, now we are going to write some programs using symbolic constants. Study them keenly.

Program 12: Write a program to find the net salary if the basic salary is input through the keyboard.
#include <stdio.h>
#include <conio.h>
void main()
{
    float bas_sal,net_salary;
    clrscr();
    printf(“\nEnter basic salary:”);
    scanf(“%f”,&bas_sal);
    #define DA 2500
    #define HRA 1500
    #define PF 2500
    net_salary=bas_sal+DA+HRA-PF;
    printf("\nThe net salary is=%f”,net_salary); 
    getch();
}

Output:
#-------------------------------------------------------------------------------#
Enter basic salary:2300
The net salary is=3800.000000
#-------------------------------------------------------------------------------#
Here, we define three symbolic constant namely DA,HRA and PF. Note that we define these constants inside main() but before using them.

Program 13: WAP to find the area of circle without using preprocessor directives.
#include <stdio.h>
#include <conio.h>
void main()
{
    float radius,area;
    clrscr();
    printf(“\nEnter the radius of circle:”);
    scanf(“%f”,&radius);
    area=3.14159*radius*radius;
    printf("\nArea of circle=%f”,area);
    getch();
}
Output:
#-------------------------------------------------------------------------------#
Enter the radius of circle:4.5
Area of circle=63.617199
#-------------------------------------------------------------------------------#

Now, we are going to write same program using preprocessor directives.
#include <stdio.h>
#include <conio.h>
#include <math.h>

#define BEGIN main(){
#define END getch();}
#define CLS clrscr();
#define  DISPLAY printf(
#define  ENDDISPLAY );
#define INPUT(K) scanf(“%f”,&K);

BEGIN
    float radius,area;
    CLS
    DISPLAY “\nEnter the radius of circle:” ENDDISPLAY
    INPUT(radius)
    area=M_PI*radius*radius;
    DISPLAY “\nArea of circle=” ENDDISPLAY
    DISPLAY “%f”,area ENDDISPLAY
END
Output:
#-------------------------------------------------------------------------------#
Enter the radius of circle:4.5
Area of circle=63.617252
#-------------------------------------------------------------------------------#

The constant M_PI used in the above program is inbuilt symbolic constant defined in the header file “math.h”. There are various PI, logarithms and other constants are declared in “math.h”. You can use the following PI constants in your program.

Common PI constants   
M_PI            :p
M_PI_2        : p/2
M_PI_4        : p/4
M_1_PI        : 1/p
M_2_PI        : 2/p
M_1_SQRTPI: 1/Öp
M_2_SQRTPI: 2/Öp

Tip: For other constants please refer to “constant data types and global variables” section of “math.h”. To do this type math.h in an empty area of your program window, press CTRL+F1 by placing the cursor at the first character i.e. m.

How preprocessor works?
The preprocessor replaces all the macro symbols used in the program by their corresponding values before the compilation operation.
For example, the statement,
          area=PI*radius*radius;
is transferred as
          area=3.14159*radius*radius;

Note: The content of the quoted strings are never changed by the preprocessor.

For example, in the statement
          printf(“PI=%f”,PI);
only the second PI in printf will be changed by the above #define directives to the preprocessor. And so, the statement output will be:
          PI=3.14159

Redefining symbolic constants
The definition of the symbolic constant can be changed by the new definition. For instance the symbolic constant PI can be redefined as:
#define PI (22.0/7)   

Look at the following code which demonstrates this:
#include <stdio.h>
#include <conio.h>
#define FLOWER “LOTUS”
void main()
{
    clrscr();
    printf(FLOWER);
    printf(“  is a national flower.”);
    printf("\n");
    #define FLOWER “ROSE -”
    printf(FLOWER);
    printf(“ I am king of flowers.”);
    getch();
}
Output:
#-------------------------------------------------------------------------------#
LOTUS is a national flower.
ROSE – I am king of flowers.

#-------------------------------------------------------------------------------#

No comments:

Post a Comment