The C preprocessor Flashcards
Source Code:
The C program
C preprocessor:
Processes source code before passing it to compiler.
It creates Expanded Source Code (filename.I)
directives:
its 4 types u know?
C preprocessor Commands. Begin with # Preferred to place them at top. types: 1. Macro expansion. 2. File Inclusion. 3. Conditional Compilation. 4. Miscellaneous directives.
Build process:
Steps from writing C program to executing it.
Macro Expansion:
its rules and terminology.
define UPPER 25
macro definition: or just macro:
during preprocessing,when the preprocessor finds #define directive, it searches for macro templates in entire program, on finding one it replaces it with its macro expansion.
UPPER : Marco Templates. customary use capital letters.
25 : their corresponding macro expansion
template n expansion separated by space or tabs.
Never terminate macro definition by a semicolon.
Why use #define directive than its other alternatives.
variables at times
- readability.
- to just make the change once instead of finding every occurrence of constant and then changing it.
Why prefer macro? cuz using vars is a bad idea for foll. 3 reasons:
-Its inefficient, Compiler generates faster and compact code for constants than variables.
- Encourages sloppy thinking, cuz using a variable for a constant.. it never changes.. hard to think of it as a variable.
- Danger, a variable might inadvertently get altered somewhere.
Some uses of C #define directive:
- to replace certain symbols by words.
- to replace conditions.
- to replace entire statement.
Macros with Arguments:
define area(x) (3.14xx)
x in macro template is argument tht matches x in macro expansion. exp will be substituted for all area(x).
Points to remember while writing macros with argument
or
Rules
- No blanks btw arg and macro template, otherwise arg will become a part of expansion.
- Enclose the macro expansions always by paranthesis. (precedence, results unexpected)
is multiple line macro a thing?
using a back slash \ at the end it is possible.
#define HLINE for(i=0;i<79;i++ )\ printf( "%c",196);\ }
Debugging Macro
View expanded source code to check how macro is getting expanded.
At command prompt generate that file by:
cpp filename.c
filename.I will be now in C:\TC\BIN directory.
difference between Macros and Functions:
Preprocessors literally replace macro templates with macro expansion.
As against this, in a function call control is passed to a function along with arguments, some calculations are performed in the function and the useful value returned.
When to use function
when to use macro
why?
If Macro is short then use macro as it does take time to pass args and getting back returned value and hence function slows down the program. Macro would make a nice shorthand.
If macro is fairly large then replace it with the function. As macro makes program run faster but increase file size, function makes smaller and compact program.
Macro used 100 times, would go and expand 100 times increasing size, but func is used, then even if called from 100 diff places, it would take same amount of place in program.
File Inclusion:
#include "filename" : Looks for file in current directory and in specified list of directories as mentioned in the include search path, which can be set up (every compiler has its way).
#include : looks for the file only in the specified list of directories.
Command to include a file in another. Entire contents of filename are inserted in source code at that point in program.
when and why is this used?
file inclusion
- Modular program. When a program is large it is divided into groups of similar functions and all are included at the top using file inclusion in the main program.
- Some functions and macro definitions are frequently used so we store it in a file and that file is included in every program.