C PROGRAMMING
MULTIPLE CHOICE QUESTION
OLD QUESTION BANK
SAMPLE QUESTION 2080 AND SOLUTION

2.5. Preprocessor Directives


Before understanding the concept of preprocessor directives, Let us first write our FIRST C PROGRAM.

Here are some rules to be followed while writing C program:

- Each instruction in a C program is written as a separate statement.

- The statements in a program must appear in the same order in which we wish them to be executed.

- Blank spaces may be inserted between two words to improve the readability of the statement.

- All statements should be in lower case letters.

- C has no specific rules for the position at which a statement is to be written in a given line. That’s why it is often called a free-form language.

- Every C statement must end with a semicolon ( ; ). Thus ; acts as a statement terminator.

FIRST C PROGRAM

Question: Write a C program to calculate simple interest for given principal, number of years and rate of interest.

#include<stdio.h>
int main()
{
    // variable declaration 
    int principal, num_year;
    float roi,si;
    // variable initialization 
    principal = 2500;
    num_year = 2;
    roi = 7.5;
    // calculate simple interest
    si = (principal * num_year * roi) / 100;
    printf("\nHere is calculation details:\n");
    printf("\nPrincipal=%d\nRate of interest=%f\nNumber of year=%d\n", principal, roi, num_year);
    printf("\nSimple Interest=%f\n",si);
    return 0;
}

Now, Let us understand each statement of above C code:

  • Execution of any C program starts at the main() function, so the main() function is called the entry point of the C program. All the statements and instruction to be executed need to be enclosed within the curly braces {...} of main function. 

In a C program, the main() function is typically defined with a return type of int. The return value of the main() function is used to indicate the exit status of the program to the operating system.

The C standard specifies that the main() function should return an integer. The integer returned by main() is often used to convey the exit status of the program to the calling environment. A return value of 0 usually indicates successful execution, while a non-zero value typically indicates an error or some other abnormal termination.

  • Variables must be declared before its use in C program

            int principal, num_year;

             float roi,si;

Here we have declared two integer variables principal and num_year and two float variables roi and si.

Note: all counting values are stored as integer variables and measurement quantities are stored in floating point variables. 

  • The following statement is for variable initialization, initialization is the process to assign value to variables.

   // variable initialization

   principal = 2500;

   num_year = 2;

   roi = 7.5;

  • Different types of operators are used to perform calculations like : + , - , *, = etc. as shown below:

si = (principal * num_year * roi) / 100;

  • printf() function is a built-in library function, it is used to display calculated results and variables to the output screen. Definition of printf() function is available in the “stdio.h” header file. To use printf() in the c program the header file need to be included as preprocessor directive in the c program as #include<stdio.h>

Preprocessor and Directives


The C preprocessor is a collection of special statements, called directives, that are executed at the beginning of the compilation process. Preprocessors directives usually appear at the beginning of a program. A preprocessor directive may appear anywhere within a program. Preprocessor directives follow special syntax rules that are different from the normal C syntax. They all begin with the symbol # in column one and do not require a semicolon at the end. We have already used the directives #define and #include to a limited extent. A set of commonly used preprocessor directives and their functions are listed below:      

Directives Functions

#define

#undef

Defines a macro substitution

Undefines a macro

#include

Species the files to be included

#ifdef

Test for a macro definition

#endif

Specifies the end of #if

#ifndef

Tests whether a macro is not defined

#if

Tests a compile-time condition

#else

Specifies alternatives when #if test fails.

Symbolic Constants

A symbolic constant is a name that substitutes for a sequence of characters that cannot be changed. The character may represent a numeric constant, a character constant, or a string. When the program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence. They are usually defined at the beginning of the program. The symbolic constants may then appear later in the program in place of the numeric constants, character constants, etc., that the symbolic constants represent.

For example:

C program consists of following symbolic definition: 

#define PI 3.141593

#define TRUE 1

#define FALSE 0

#define PI 3.141593 defines a symbolic constant PI whose value is 3.141593. When the program is preprocessed, all occurrences of the symbolic constant PI are replaced with the replacement text 3.141593.

C program to calculate area of circle demonstarting concept of preprocessor directive #define and #include:

#include <stdio.h>
// Define a constant for the value of PI
#define PI 3.14159

int main() {
    // Input: radius of the circle
    double radius=5.25;
    // Calculate the area of the circle
    double area = PI * radius * radius;
    // Output: Display the calculated area
    printf("The area of the circle with radius %.2f is: %.2f\n", radius, area);
    return 0;
}