Preprocessors in C PDF
Document Details
![ComplimentaryJasper2269](https://quizgecko.com/images/avatars/avatar-2.webp)
Uploaded by ComplimentaryJasper2269
Tags
Summary
This document provides an overview of preprocessor directives in C programming. It details the different types of preprocessor directives, including macro substitution, file inclusion, and conditional compilation, along with illustrative examples for each.
Full Transcript
Preprocessors in C ================== - Preprocessor directives in C are statements that are processed before compilation begins. They are usually placed at the beginning of a program, but can be used anywhere. - Preprocessor directives are startswith **\#** symbols (Hash). - The pr...
Preprocessors in C ================== - Preprocessor directives in C are statements that are processed before compilation begins. They are usually placed at the beginning of a program, but can be used anywhere. - Preprocessor directives are startswith **\#** symbols (Hash). - The preprocessor in C is also known as a **macro processor** Types of C Preprocessors ------------------------ - There are mainly 3 Types of Preprocessor Directives , they are: 1. 2. 3. 1. **Macro Substitution:** Macros are pieces of code in a program that is given some name. Whenever this name is encountered by the compiler, the compiler replaces the name with the actual piece of code. The **'\#define'** directive is used to define a macro. **[Syntax:]** \#define token value **OR** \#define first\_part second\_part **[Example:]** \#define SIZE 10 **[Note:]** There is no semi-colon (;) at the end of the macro definition. Macro definitions do not need a semi-colon to end. **[Program:]** \#include \ // File Inclusion \#define LIMIT 5 // macro definition int main() { for (int i = 0; i \< LIMIT; i++) { printf(\"%d \\n\", i); } return 0; } 2. **File Inclusion:** ------------------- This type of preprocessor directive tells the compiler to include a file in the source code program. The **\#include** preprocessor directive is used to include the header files in the C program. **[Syntax: ]** **\#include \** Here, The **'\'** brackets tell the compiler to look for the file in the standard directory. **\#include \"filename\"** Here, The double quotes ( **" "** ) tell the compiler to search for the header file in the source file's directory. 3. **Conditional Compilation:** ---------------------------- **\#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. - It execute a specific portion of the program or to skip the compilation of some specific part of the program based on conditions. +-----------------------------------+-----------------------------------+ | **[Syntax:]** | **[Syntax:]** | | | | | **\#ifdef** macro\_name | **\#ifdef** macro\_name | | | | | //code | Statement1 | | | | | **\#endif** | **\#else** | | | | | | Statement2 | | | | | | **\#endif** | +-----------------------------------+-----------------------------------+ **[Program:]** \#include\ \#include\ \#define X 10 void main() { clrscr(); \#ifdef X printf(\"one\"); \#else printf(\"two\"); \#endif getch(); } **Output:** one **Program:** +-----------------------------------+-----------------------------------+ | 1. \#include \ | \#include \ | | | | | 2. \#include \ | \#include \ | | | | | 3. \#define NOINPUT | void main() | | | | | 4. **void** main() | { | | | | | 5. { | int a=0; | | | | | 6. **int** a=0; | clrscr(); | | | | | 7. clrscr(); | \#ifdef NOINPUT | | | | | 8. \#ifdef NOINPUT | a=2; | | | | | 9. a=2; | \#else | | | | | 10. \#else | printf(\"Enter a:\"); | | | | | 11. printf(\"Enter a:\"); | scanf(\"%d\", &a); | | | | | 12. scanf(\"%d\", &a); | \#endif | | | | | 13. \#endif | printf(\"Value of a: %d\\n\", a); | | | | | 14. printf(\"Value of a: %d\\n\", | getch(); | | a); | | | | } | | 15. getch(); | | | | **Outout:** Enter a: | | 16. } | | | | 5 | | Output: Value of a:2 | | | | Value of a:5 | +-----------------------------------+-----------------------------------+ **Note:** if you don\'t define macros it goes to after \#else statement **[\#if:]** ======================= 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. **Syntax:** +-----------------------+-----------------------+-----------------------+ | 1. 2. **\#if **expr | 1. **\#if** expressi | 1. **\#if** expressi | | ession | on | on | | | | | | 3. //code | 2. //if code | 2. //if code | | | | | | 4. **\#endif** | 3. **\#else** | 3. **\#elif** expres | | | | sion | | | 4. //else code | | | | | 4. //elif code | | | 5. **\#endif** | | | | | 5. **\#else** | | | | | | | | 6. //else code | | | | | | | | 7. \#endif | +-----------------------+-----------------------+-----------------------+ **Program:** \#include \ \#include \ \#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(); } [\#undef:] ====================== - The **\#undef** preprocessor directive is used to undefine the constant or macro defined by **\#define**. - The \#undef directive is used to define the preprocessor constant to a limited scope. 1. **Syntax:** **\#undef** token 2. +-----------------------------------+-----------------------------------+ | \#include \ | \#include \ | | | | | \#define PI 3.14 | \#define number 15 | | | | | \#undef PI | int square=number\*number; | | | | | void main() | \#undef number | | | | | { | void main() | | | | | printf(\"%f\",PI); | { | | | | | } | printf(\"%d\",square); | | | | | **OUTPUT:** Compile Time Error: | } | | \'PI\' undeclared | | | | **OUTPUT:** 225 | +-----------------------------------+-----------------------------------+