Home  >  Article  >  Backend Development  >  What are preprocessor commands in C language?

What are preprocessor commands in C language?

WBOY
WBOYforward
2023-08-29 19:49:06807browse

What are preprocessor commands in C language?

A preprocessor is a program that sends source code before it passes through the compiler. It operates according to preprocessing directives starting with the # symbol.

Types

There are three types of preprocessor commands, as follows:

  • Macro replacement directives.

  • File contains directives.

  • Compiler control directives.

Macro replacement directive

It replaces each occurrence of an identifier with a predefined string.

The syntax for defining a macro replacement directive is as follows:

# define identifier string

For example,

#define    PI    3.1415
#define    f(x)  x *x
#undef     PI

Example

The following is a C program for a macro replacement directive−

#define wait getch( )
main ( ){
   clrscr ( );
   printf ("Hello");
   wait ;
}

Output

When the above program is executed, it produces the following results−

Hello

File Include Directive

You can use the #include directive to include external files that contain function (or) macro definitions.

The syntax of the file inclusion directive is as follows:

# include <filename> (or) #include "filename"

Example

The following is a C program for the file inclusion directive:

Real-time demonstration

#include <stdio.h>
main ( ){
   printf ("hello");
}

Output

When the above program is executed, it produces the following results −

Hello

The function printf() is defined in the header file.

Compiler Control Directives

The C preprocessor provides a feature called conditional compilation, which can be used to turn on (or off) a specific line (or group of lines) in a program ).

Example

The following is a C program of compiler control instructions:

Real-time demonstration

#if, #else, #endif etc.
#define LINE 1
#include<stdio.h>
main ( ){
   #ifdef LINE
   printf ("this is line number one");
   #else
   printf("This is line number two");
   #endif
}

Output

When the above program is executed , it produces the following result −

This line number one

The above is the detailed content of What are preprocessor commands in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete