Home > Backend Development > C++ > body text

What are preprocessor commands in C language?

WBOY
Release: 2023-08-29 19:49:06
forward
807 people have browsed it

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
Copy after login

For example,

#define    PI    3.1415
#define    f(x)  x *x
#undef     PI
Copy after login

Example

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

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

Output

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

Hello
Copy after login
Copy after login

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"
Copy after login

Example

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

Real-time demonstration

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

Output

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

Hello
Copy after login
Copy after login

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
}
Copy after login

Output

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

This line number one
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template