Subscribe by Email

Saturday, September 3, 2011

What is a C PREPROCESSOR ? Why is it important?

C preprocessor or cpp is a program that is executed prior to the execution of the compiler. It’s very important as it removes all the comments from the source and performs textual substitution based on the code and passes it to the compiler for actual compiling and it also handles directives for the inclusion of source files. Consider the following statement:
This is the way all macros and defines are declared. Before the start of compilation, the preprocessor resolves all of the defines. Macro is also a kind of define, but it is capable of appearing to perform some logical operations, math’s functions and it has a unique name. While defining a macro, it is imperative that there should be no space between the name of the macro and opening parentheses. If a space is present, it makes it difficult for the compiler to determine whether its macro or not. Instead it’ll handle it like a simple substitution define statement.
Often there are a lot of extra parentheses in a macro definition but they are extremely important. Most experienced C programmers always put parentheses around each and every variable in a macro and entire expression. This avoids error and allows any macro to work properly. A macro cannot be expanded within a quoted string, although, the arguments’ text can be quoted and that will be treated as a string literal. This can be done using “#” directive. There are macros called variadic macros which can take a varying number of arguments. They are useful when writing wrappers to variables of number functions. X macro is a header file which contains a list of similar macro calls. Macros are of two types namely object-like and function-like.
Object like macros do not accept parameters whereas function like macros do. Function like macro should not have a whitespace between the variable and the opening parentheses. Presence of a whitespace will make the compiler treat it as an object like macro. Object like macros are conventionally used to create symbolic names for constants and are generally considered as part of good programming. You can extend a macro across many lines by using a back slash escape sequence at the end of every line. The ternary conditional operator “?:” is also a function like macro. Token pasting or concatenation is another very easy to abuse feature of C preprocessor. Using this we can join together two arguments using ## preprocessor. This is basically used to create more elaborated macros. Multiple statements macros must be avoided as far as possible because they can show unexpected behavior. We can make a macro safe by replacing the semicolon with comma since it has lowest precedence.
Conditional compilation is a very useful construct and is used to include a feature if we are using a certain processor, a certain operating system and certain hardware. The following are the directives that can be used for conditional compilation: #if, #ifdef, #ifndef, #else, #elif and #endif. More trivial programs are too large and cannot be place within a single file and it becomes very cumbersome to work with. Therefore, it becomes very necessary for these files to work and communicate together as one large program.
What do we mean by pragma? It’s a kind of instruction to the compiler to perform a particular action at the time of compilation. The pragmas vary from compiler to compiler since they are not standardized. If the compiler provides a source listing file, then it does have pragmas to format output. #ptragma is used suppress specific error messages, to stack debugging and to manage heaps.

No comments:

Facebook activity