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.
Conditional Compilation:
To have the compiler skip over part of a source code by inserting preprocessing commands #ifdef and #endif.
#ifdef macroname statement(s); #endif
[ include #define macroname and it will proceed as usual]
When would u like to compile only a part of your program:
- Clients usually want to undo the change they had asked, in that case to “comment out” the code we cant use comment as it might have comments and we cant have nested comments in c, so we use #ifdef.
- To make the program portable on diff machines.
- To avoid ‘Multiple declaration of a function’.
How to make programs portable?
To make programs portable:
DOUBT LOOK INTO IT LATER
conditional compilation directives:
- # ifdef MACRONAME… #else… #endif
- # ifndef MACRONAME .. #else….. #endif
3. #if Condtion ... #elif condition... #else .... #endif #elif reduces number of #endif Condition of MACRONAMES. if true (non zero) then corresponding block compiled.
Miscellaneous Directives:
- # undef
2. #pragma
undef directive:
#undef macro template Undefines that macro template and brings changes in conditional compilation following this.
pragma directives:
1. #pragma startup functionName #pragma exit finctionName
These function cannot receive nor return any value.
declare those functions prototypes above,
startup executes at the beginning of the program before main and exit allow the respective function to execute before the program terminates.
If u want two functions to get executed at startup then their pragmas should be defined in reverse order in which u want them to get called.
- # pragma warnsuppress any warnings generated by compiler.
2 most common:- bad programming. not using void for non returning function.
- Writting code that might cause runtime error. Like assigning uninitialised pointer.
pragma warn
#pragma warn -rvl /* return vlaue */ #pragma warn -par /* parameter not used */ #pargma warn -rch /* unreachable code */
if - replaced by + then earnings are showed. THis is used when we want to just focus on errors and look at wearing later.
The Build Process:
C Source Code [filname.C) goes through preprocessor and results in
Expanded SOURCE CODE (filname.i)
goes through compiler and gives
ASSEMBLY CODE (filname.ASM) goes through assembler and gives
RELOCATABLE OBJECT CODE
goes through linker along with object code of library functions and gives
EXECUTABLE CODE (filname.EXE)
this is sent to LOADER to execute.