Constants In C Language

No comments
In c language constants are the values which are fixed and do not change during the execution of a c program.Constants are also called literals sometimes in C.We cannot change the value of a constant during the execution of a C program but the value of a variable can be changed during the execution.
For example-
Int x=3; //value of x is 3 
X=2; //value of x changes to 2 

In above example the value of the variable x is 3 in the first line but as the execution reaches on the second line the value of the same variable x becomes 2.But the values of the constants 2, and 3 does not change.


Classification Of constants In C-

Type of Constant Value stored
Integer Integer values can be stored
Character It can store character values
Float It can store Float values
String It can store String values

How to define a constant in C-

In c programming language there are two ways for defining a constant-
  • Defining a C constant using #define pre-processor directive

  • Defining a C constant using const keyword.

1. Defining a C constant using #define preprocessor directive-

C language preprocessor directive #define can be used for defining a constant in C.

Basic Syntax for defining a constant-

#define CONSTANT_NAME value
Example- #define DRINKING_AGE 18
In above example #define is a preprocessor command which is followed by the constant name and then the value of the constant which is 18 remains fixed during all the execution process of the program.

How defining a constant with #define pre-processor directive works-

In our program we use the constant at various places with the help of its symbol, representing the value of constant. The pre-processor finds that symbol in our whole code till the end and replace it with the value of the symbol in the whole program.

A perfect example on defining and using constants in C (Using #define pre-processor directive)-

Program-
   #include <stdio.h>
   #include <conio.h>

#define STUDENTS_CLASSA 55
#define STUDENTS_CLASSB 60
#define FINE_PERSTUDENT 100

void main(){

int total_fine;

int fine_classA=STUDENTS_CLASSA*FINE_PERSTUDENT;
printf("fine of class A is %d\n" ,fine_classA);

int fine_classB=STUDENTS_CLASSB*FINE_PERSTUDENT;
printf("fine of class B is %d\n" ,fine_classB);

total_fine = fine_classA + fine_classB;
printf("total fine collected is %d\n" ,total_fine);
} 

After compilation and execution of above program-
Output
fine of class A is 5500
fine of class B is 6000
total fine collected is 11500

2. Defining a C constant using const keyword-

For defining a constant we can use a const keyword before the type of the constant and name representing the constant. BASIC SYNTAX-
const datatype CONSTANT_NAME = value;
Example-
const int DRINKING_AGE =18;

A perfect example on defining and using constants in C(using const keyword)

Program-
   #include <stdio.h>
   #include <conio.h>

void main(){

const int STUDENTS_CLASSA = 55;
const int STUDENTS_CLASSB = 60;
const int FINE_PERSTUDENT =100;

int total_fine;

int fine_classA=STUDENTS_CLASSA*FINE_PERSTUDENT;
printf("fine of class A is %d\n" ,fine_classA);

int fine_classB=STUDENTS_CLASSB*FINE_PERSTUDENT;
printf("fine of class B is %d\n" ,fine_classB);

total_fine = fine_classA + fine_classB;
printf("total fine collected is %d\n" ,total_fine);

}

After compilation and execution of above program-
Output
fine of class A is 5500 fine of class B is 6000 total fine collected is 11500

Things to remember for defining constants-

  • It is recommended to write the constant name in caps , so that we can easily compare between variables and constants.

  • Never use a semicolon in the end of declaration statement unless you want to include the semicolon as a part of constant. (in case of using #define only).

  • The rules for naming a constant representing keyword (CONSTANT_NAME) are same as variables.

  • The symbol should be a single word.

Advantage of using a constant–

Suppose in the above example if government changes the drinking age from 18 to 20, then we can easily change it only at one place (place of declaration) and it will apply to the whole code itself when we will compile the program next time. Think about changing the value manually in an application with thousands of files, it would be a very difficult task.

No comments :

Post a Comment