C Preprocessor

 

Introduction to C Preprocessor

The Preprocessor used in C is one of the important step which is used in the compilation process but it is not a part of the compiler. In simple terms, a preprocessor is system software or a program which process the source code before the compilation step.

C Program Compilation:

A C Preprocessor performs processing of source code or high-level language (HLL).
The first step in the language processing system is preprocessing. This language processing system converts the high-level language into a language that is easily understood by the machine, i.e., machine-level language.

The intermediate steps involved between writing a C program and it’s execution are shown in the figure above.

A preprocessor is primarily used for performing three tasks on high level language(HLL) code given below:

  1. Expanding macros: The preprocessor replaces occurrences of macro names with their corresponding definitions.
  2. Including files: The preprocessor inserts the contents of specified files into the source code at the point where the #include directive appears.
  3. Conditional compilation: The preprocessor can include or exclude parts of the source code based on specified conditions, using the #if, #ifdef, and #ifndef directives.
  4. Replacing constants: The preprocessor replaces occurrences of constants with their corresponding values, using the #define directive.

C Preprocessor Directives


All preprocessing directives begin with a hash symbol(#).

Some of the commonly used preprocessor directives are listed below:

DirectiveDescription
#defineThis directive substitutes a preprocessor macro.
#includeThis directive inserts a particular header from another file.
#undefThis directive undefines a preprocessor macro.
#ifdefThis directive returns true if this macro is defined.
#ifndefThis directive returns true if this macro is not defined.

Example


#include <stdio.h>
#define PI 3.1415

 int main () 
{
  
float r, a;
  
printf ("Enter the radius: ");
  
scanf ("%f", &r);
  
    // Notice, the use of PI
    a = PI * r * r;
  
 
printf ("Area=%.2f", a);
  
return 0;
}

Output


Enter the radius: 6
Area = 113.09

netaji gandi Friday, July 18, 2025
C Program to check if a Number Is Positive Or Negative

 

C Program to check if a Number Is Positive Or Negative



#include <stdio.h>
int main()
{
    int num = 23;
      
    //Conditions to check if the number is negative/positive or zero
    if (num > 0)
         printf("The number is positive");
    else if (num < 0)
        printf("The number is negative");
    else
        printf("Zero");
    
    return 0;
}                                                                                                                                                                                       

Output:

Insert a number: 23
The number is Positive                                                               
#include <stdio.h>
int main()
{
    int num = -10;
    
    //Condition to check if num is negative/positive or zero
    if (num >= 0)
    {
        if (num == 0)
            printf("The number is 0");
        else
            printf("The number is Positive");
    }
    else
        printf("The number is Negative");
    
    return 0;
}

Output

Insert a number: -10
The number is Negative
#include <stdio.h>
int main()
{
    int num = -4;
    
    //Condition to check if the 0, positive or negative
    
    if(num == 0)
        printf("Zero");
    else
        (num > 0) ? printf("Positive"): printf("Negative");
    
    return 0;
}
Insert a number: -4
Negative

netaji gandi Tuesday, July 15, 2025

2025-26 MCA OBJECT ORIENTED PROGRAMMING WITH JAVA (MCA1105)

OOPS with JAVA (MCA1105) R20 MCA Syllabus – Complete Details & Unit-wise Material Course Objectiv...