C Tutorial

Define C

Define Programming

Programming Approach

First C Program

TurboC Shortcut keys

Compiler vs Interpreter

C Variable

C Keywords

C Data Types

C Comments

C Operators

Hierarchy of Operators

Ex: Arithmetic Operator

C Formatting Output

C Escape Sequence

C if statement

Ex: If statement

C Switch statement

Ex: Switch

Increment / Decrement

C Loops

Ex: Loops

C Nesting Of Loops

Ex: Nested Loops

C Jumping Statements

C Exit(), Gotoxy()

C Arrays 1D

Ex: Arrays 1D

C Arrays 2D

Ex: Arrays 2D

C Sorting

ASCII Value

Character I/O Function

Ex: Character

C String Functions

Ex: Strings

Array of Strings

C Math Functions

User-defined Function

Exercise Function

Local, Reference variable

Function Calling types

Array Passing Function

Recursion Function

Ex: Recursion Function

Constant Variable

Storage Class

C Header Files

C Preprocessor

C Pointers

Pointer to Pointer

C Structures

Exercise Structure

C Typedef

C Enumeration

C File Handling

Ex: File Handling

Command Line Argument

MCQ

Question-Answer

Page Stats

Visitor: 613

C - PreProcessor

C - PreProcessor process our source code before it is passed to the compiler. Each preprocessor statement begins with hash '#' symbol.

List of important preprocessor directives
Directive Description
#include Inserts a particular header file in our program.
#define Substitutes a preprocessor macro.
#undef Undefines a preprocessor macro.
#ifdef Returns true if this macro is defined.
#ifndef Returns true if this macro is not defined.
#if Tests if a compile time condition is true.
#else The alternative for #if
#elif #else an #if in one statement
#endif Ends preprocessor conditional
#error Prints error message on stderr
#pragma Issues special commands to the compiler, using a standardized method

#include: The #include preprocessor directive is used to include system-defined and user-defined header files. If included file is not found, compiler renders error.

#define: The #define preprocessor directive is used to define constant or micro. It can use any basic data type.

#define pi 3.14  declare constant variable pi with 3.14 value.
#define and &&  declare && operator with english word and.
#define or ||  declare || operator with english word or.

Macro with Argument: Macro's can have argument just like functions. Syntax to define Macro's with argument is:

#define sum(a,b) a+b  declare a macro sum with two arguments to calculate sum of two numbers.
#define area(x) 3.14*r*r  declare a macro area with one argument and also define its defination in one line.
#define sq(a) a*a  declare a marcro sq with one argument to calculate square of a number.
#define greater(a,b) (a>b)?a:b  Input 2 number and find greater
#define greater3(a,b,c) (a>b && a>c)?a:(b>a && b>c)?b:c  Input 3 number and find greater.

#undef: The #undef preprocessor directive is used to undefine the constant or macro. Syntax:

#undef  FILE_SIZE //undefine the macro
#define FILE_SIZE 42 //redefine the macro

#ifdef: The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code otherwise #else code is executed, if present. Syntax:

#define NOINPUT //remove this line and execute code again
void main() 
{  
    int a=0;  
    #ifdef NOINPUT  
        a=2;  
    #else  
        printf("Enter a:");  
        scanf("%d", &a);  
    #endif         
    printf("Value of a: %d\n", a);  
    getch();  
}

#ifndef: The #ifndef preprocessor directive checks if macro is not defined by #define. If yes, it executes the code otherwise #else code is executed, if present. Syntax:

#define INPUT  //remove this line and execute code again
void main() 
{  
    int a=0;  
    #ifndef INPUT  
        a=2;  
    #else  
        printf("Enter a:");  
        scanf("%d", &a);  
    #endif         
    printf("Value of a: %d\n", a);  
    getch();  
}

Predefined Macros: ANSI C defines many predefined macros that can be used in c program.

Macro Description
__DATE__ The current date as a character literal in "MMM DD YYYY" format
__TIME__ The current time as a character literal in "HH:MM:SS" format
__FILE__ This contains the current filename as a string literal.
__LINE__ This contains the current line number as a decimal constant.

Example: Predefined Macro

void main()
{
    printf("File :%s\n", __FILE__ );
    printf("Date :%s\n", __DATE__ );
    printf("Time :%s\n", __TIME__ );
    printf("Line :%d\n", __LINE__ );
}

#if preprocessor: The #if preprocessor directive evaluates the expression or condition. If condition is true, it executes the code otherwise #elseif or #else or #endif code is executed.

#define NUMBER 1  
void main() 
{
    #if NUMBER==0  
        printf("Value of Number is: %d",NUMBER);  
    #else  
        print("Value of Number is non-zero");  
    #endif         
    getch();  
}

#error preprocessor: The #error preprocessor directive indicates error. The compiler gives fatal error if #error directive is found and skips further compilation process.

#include<stdio.h>
#ifndef __MATH_H  
    #error First include then compile  
#else  
void main()
{  
   float a;  
   a=sqrt(7);  
   printf("%f",a);  
}  
#endif
Compile Time Error: First include then compile
[if you include math.h, it does not gives error.]

Miscellaneous Directives

#pragma directive
a) #pragma startup This directive allow us to execute function before main() function. Syntax:

void fun()
{
    printf("Welcome, user");
}
#pragma startup fun
void main()
{
    printf("\nMain Message");
}

We can use more than one function to be executed at startup but they will be executed in reverse order.

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

void fun1()
{
    printf("\nWelcome, user1");
}
void fun2()
{
    printf("\nWelcome, user2");
}
#pragma startup fun1
#pragma startup fun2
void main()
{
    printf("\nMain Message");
}

0 = Highest priority
0-63 = Used by C libraries
64 = First available user priority
100 = Default priority
255 = Lowest priority

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

void fun1()
{
    printf("\nWelcome, user1");
}
void fun2()
{
    printf("\nWelcome, user2");
}
#pragma startup fun1 64
#pragma startup fun2
void main()
{
    clrscr();
    printf("\nMain Message");
    getch();
}

b) #pragma exit This directive allow us to execute function after main () function.

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

void fun()
{
    printf("Thankyou for using the program");
}
#pragma exit fun
void main()
{
    clrscr();
    printf("\nMain Message");
    getch();
}

Both startup and exit functions should neither receive nor return any value.
#pragma warn This directive tells the compiler whether the warning will be shown or not.
a) #pragma warn –rvl return value warning does not display even if return keyword is not used in a function.
b) #pragma warn – par parameter not used warning does not display even if variable not used in a parameter.
c) #pragma warn – rch unreachable code warning does not display even if any code of statement is unreachable.

Updated: 12-Jul-21