Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

Preprocessors In C

Loading...

Published in: C / C++
5,126 Views

Definition of preprocessor. Types of preprocessors. Examples.

Ashish / Gwalior

17 years of teaching experience

Qualification: M.Tech. (Software Systems)

Teaches: Computer, C / C++, C# (C Sharp), Java And J2EE, Visual Basic, Java Script, PHP And MySQL

Contact this Tutor
  1. PreprocessOrS
  2. Definition Preprocessors are some special commands that start with hash (#) symbol. Preprocessing is done before compilation and it generates expanded source code. •Following diagram show the concept of preprocessing :
  3. Source Code .C/.CPP File preprocessing Expanded Source Code Compiling Object Code Linking Executable Code .OBJ File .EXE File
  4. Important Preprocessors 2) 3) 4) 5) #include # ifdef # if n def # undef
  5. • It can be used in 3 ways It can be used to define constants. 1) It can be used to create shortcuts. 2) 3) It can be used like a small function (macro).
  6. Example 1 — Defining Constants #include # define N 10 void main() int x; printf("%d" x) •
  7. Output 50
  8. Example 2 — Creating Shortcut #include # define P printf("Enter a number :") void main() int x; printf("Value of x = %d" x)'
  9. Output Enter a number : 25 Value of x = 25
  10. Example 3 - Macro #include # define ADD(a,b) a+b void main() int x; printf("%d" x) •
  11. Output 30
  12. #include • This preprocessor is used to include header files in our programs. • Header files contain functions and macros. • The code of header file is attached with our program after preprocessing. So we can use all the things defined in the header file in our program. • E.g. printf() function is defined in a header file called . So we need to include this header file to use printf() function in our program.
  13. # ifdef • This preprocessor is used for conditional compilation. • Syntax #ifdef MacroName Statements #else Statements #endif
  14. Example #include void main() #ifdef N printf("Hello"); #else printf("Welcome"); #endif
  15. Output Welcome
  16. #ifndef • This preprocessor is also used for conditional compilation. • Syntax #ifndef MacroName Statements #else Statements #endif
  17. Example #include void main() #ifndef N printf("Hello"); #else printf("Welcome"); #endif
  18. Output HellO
  19. • This preprocessor is used to undefine a macro. •we can also redefine the macro after undefining it.
  20. Example #include #define N 10 void main() int x; printf("%d \n" x); #undef N #define N 20 printf("%d \n" x);
  21. Output 50 100