0% found this document useful (0 votes)
3 views

pre-processor

Preprocessor directives are instructions processed by the compiler before compilation, commonly used in C and C++ for tasks like file inclusion and macro definitions. Key directives include #include for file inclusion, #define for macros, and #ifdef for conditional compilation. They help set up the environment and control the compilation process without requiring semicolons.

Uploaded by

sentilatn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

pre-processor

Preprocessor directives are instructions processed by the compiler before compilation, commonly used in C and C++ for tasks like file inclusion and macro definitions. Key directives include #include for file inclusion, #define for macros, and #ifdef for conditional compilation. They help set up the environment and control the compilation process without requiring semicolons.

Uploaded by

sentilatn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Preprocessor directives are instructions that are processed by the compiler before the actual compilation

of the program begins. They are commonly used in languages like C and C++ to handle tasks such as file
inclusion, macro definitions, and conditional compilation. Preprocessor directives typically start with the
# symbol and do not require a semicolon at the end.

Here are some common preprocessor directives:

1. #include

This directive is used to include the contents of another file, usually a header file, into the current file.

#include <stdio.h> // System header files

#include "myheader.h" // User-defined header files

2. #define

Used to define macros, which are constants or small functions that are substituted into the code during
preprocessing.

#define PI 3.14159 // Macro for constant

#define SQUARE(x) ((x) * (x)) // Macro for inline function

3. #undef

This directive is used to undefine a macro that has been previously defined using #define.

#undef PI

4. #ifdef / #ifndef / #if / #else / #elif / #endif

These directives are used for conditional compilation. They allow certain parts of the code to be
compiled or ignored based on whether certain conditions are met (such as whether a macro is defined).

#ifdef DEBUG

printf("Debugging enabled.\n");

#endif

#ifndef VERSION

#define VERSION 1

#endif
#if VERSION == 1

printf("Version 1.\n");

#elif VERSION == 2

printf("Version 2.\n");

#else

printf("Unknown version.\n");

#endif

5. #pragma

This directive provides additional information to the compiler, such as disabling certain warnings
or optimizing specific sections of code. Different compilers may support different #pragma directives.

#pragma once // Prevent multiple inclusions of a file

#pragma GCC diagnostic ignored "-Wdeprecated-declarations" // Ignore specific warning

6. #error

This directive forces the compiler to produce an error message if a certain condition is met. It’s useful for
stopping compilation when an essential condition is not met.

#ifndef PI

#error "PI is not defined"

#endif

7. #line

This directive is used to change the line number and the filename reported by the compiler, which can
be useful for debugging.

#line 100 "myfile.c"

Preprocessor directives are executed before the compiler starts compiling the actual code, which makes
them very useful for setting up the environment, including files, and conditionally compiling code.

You might also like