answersLogoWhite

0


Best Answer

As the name indicates the C PreProcessor is the preprocessor for C Programming language and is used by the compiler automatically to transform the program before actual compilation. It processes the preprocessor directives (file inclusion, macro definition and conditional compilation) in the source code. It is also known as a macro processor.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

1. Pre-Processor Directives

a. if/elif/else/endif

b. ifndef/ifdef

c. define (Macros)

d. undef

e. include

Pre-Processor directives are special symbols which are evaluated before compilation. They can be used to control how something is compiled with the #pragma directives, or the #if constructions, and the #include directive can be used to make the compiler compile files other than the ones that the programmer explicitly compiles on the command line, effectively making several files one file. These directives can be used in all manner of ways, and I will be going over each of them briefly, except for the pragma directive which is dependent upon the compiler that you use for what pragmas will be supported. None of the pre-processor directives need a ';' to end them. They end with the line they are on, though a '\' can be used to extend them beyond one line. Though if that is used, you must remember not to have anything else on that line.

#if/elif/else/endif

Next (ifndef/ifdef) Prev (Pre-Processor Directives TOC) Top (Pre-Processor Directives)

This directive has the same form as most programming languages if/else statements. Though, this is a bit more archaic than C++ in that it uses an If statement, optionally an elif (else-if) statement, optionally an else statement, and finally it requires that you close the statement with an endif statement. This is probably the most useful directive for conditional compilation that there is. This directive has the effect of either making sure certain areas of the program are not compiled in certain situations, or making sure that certain areas of the program are only compiled in certain situations.

The important thing to remember with regard to this directive is that only things which the pre-processor knows about can be used for the conditions. So, you may have constant expressions, or macros as the conditions. Basically this evaluates to the idea that you can only have things that resolve at compile time affect the compilation.

A full description of each part of the #if directive is below.

#if (condition) -- if the condition is true, then allow the code in the section to be compiled, most logical operators are allowed here, if they are given incompatible arguments, then the pre-processor will either evaluate the condition as false, or will error. A special keyword that has its own macro below is the defined() macro, which determines if something has been #define'd or not.

#elif (condition) -- if none of the #if or #elif conditions above this were true, then evaluate this condition, if it is true, allow the code in the section to be compiled.

#else -- if none of the #if or #elif conditions above this were true, then allow the code in the section to be compiled.

#endif -- end this if directive.

consist of any valid C++ code.

So, given that, the structure of these are:

#if (condition)

#elif (condition) // Note: This is an optional case...

#else // Note: This is an optional case...

#endif

The fact that this directive has optional companion directives is that you may see it in the form of:

#if (condition)

#elif (condition)

#endif

or:

#if (condition)

#else

#endif

or:

#if (condition)

#endif

ifndef/ifdef

Next ( define ) Prev (if/elif/else/endif) Top (Pre-Processing Directives)

The shortest story on these is that they are simply a macro for another directive. #ifndef should be read, if not defined, and can also be written as:

#if !defined(symbol)

Similarly, #ifdef can also be written as:

#if defined(symbol)

These two constructions both follow the rules of an #if construction and may be used the same way, with the exception that they do not allow an elif clause. They do in fact allow #else directives, but not #elif. These must also be ended with an #endif directive.

These two directives allow conditional compilation based on whether or not a symbol is defined. The catch here is that the symbol to be checked must be something that is #define'd. This is actually because of the fact that at the time that the pre-processor is active, the program is not compiled and therefore program variables cannot be accessed, even as to whether they are defined or not. Though anything that has been #define'd can be checked for definition with these directives. To check a macro (a psuedo-function which has been defined using the #define directive) one need only supply the name of the macro (no parens are needed). Further, in the #if directive the defined() macro is in fact a macro, and therefore requires parens around the symbol name to be checked, while the ifndef/ifdef construction does not. So...the example is:

#define free(x) _free(x)

#if defined(free)

#endif

#ifndef free

#endif

Both of the above would be true expressions and would therefore allow the statements in their bodies to execute.

define

This macro defines a given symbol to be something else. The exact behavior of this is that where ever the symbol that is defined appears within the program, it will be EXACTLY replaced with the contents of the macro. There are several special characters including '#' '@' and others that are not allowed in these directives, though they have a purpose for the directive, though I shall not go over those. The '\' character is also special for the #define directive, though it is used to escape any character, and one may continue a #define directives substitution string beyond the end of the line using the '\' character, because it will escape the '\n' and will not end the directive (any pre-processor directive ends with a '\n'). Unfortunately though, for this behavior the '\' must be the last character on the line, so that it really does escape the newline ('\n'). The syntax for this directive is that the directive must be the first thing on the line (just as all pre-processor directives), and then the symbol name must be next. The macro may take arguments, and in that case, the symbol should have an open paren, a naming for each argument, separated by commas, and then the closing paren. After that, the next characters are taken as the body of the macro and will be substituted into wherever the symbol is found. The body of the macro will be evaluated by the pre-processor and any named arguments to the macro will be substituted for their values, as the code specifies. An example is below:

#define free(x) _free(x)

-- what this says is that everywhere that the pre-processor finds the word free, it should replace that with _free, and further, free must have one argument to it, which is placed as the argument to _free. So, the following piece of code:

(intList is assumed to have a member named next which contains the next element in the list, and 'list' is assumed to be of the type intList).

for (intList * p = list; p; p = p->next) {

free(p);

}

Given the #define above, would look like this after the substitution:

for (intList *p = list; p; p = p->next) {

_free(p);

}

Further,

#define free(x) { char * p = "The blue moon is "; \

printf("%s %s", p, x); }

for (intList *p = list; p; p = p->next) {

free(p);

}

Would be expanded as:

for (intList *p = list; p; p = p->next) {

{ char * p = "The blue moon is ";

printf("%s %s", p, p); }

}

Which, though syntactically correct, would undermine the program, as it would no longer perform the function of the free function. Though notice that this would also not act on the p variable of the for loop, but would instead act on the p variable of the block within the for loop (a char *, not an intList*). Further remember the fact that as the macros are expanded exactly as they are defined, they may throw error messages into strange places (as they can add lines to a program, and an error can be within the macro, and yet the error will show up on the line that the macro is expanded on). Macros are a powerful tool, but they can be abused, as they may increase program size, remember they are substituted exactly, and if the macro is only five characters, but one hundred characters are substituted for it, and you use it fifty times, then you program has grown by 450 characters (5*50 = 250, 100*50 = 5000)... Most of the time, functions are better to use, the only situation where it is not the case is where something cannot be done using a function that can be done using a macro, such as the following:

class ExceptionThing : public Exception {

public:

ExceptionThing();

char * toString();

int myCode();

private:

int codeNum;

...

};

however, you have thirty of these classes, which though they all define the same functions (the front part of the class), they require different member variables, some of them have a state, some of them have a state stack (for history), and basically each one is different, but they all have the same functions.... Then, as you can see, there is no way to do a function that will produce that, however, you can use a macro, consider the following:

#define makeClass(classname) \

class classname : public Exception { \

public: \

classname(); \

char * toString(); \

int myCode();

Now the above class definition can be simplified as:

makeClass(ExceptionThing)

private:

int codeNum;

...

};

The first thing to note is that this is a hack and though it may be more trouble to do the entire definition, it is probably a better idea just because of readability. However, there are situations in which this can cut down development time, and leave the programmer to do more interesting stuff with his/her time. Further, notice about the macro itself, there are '\'s on each line, because otherwise the macro would end with the first '\n' encountered (though recall, the '\' character escapes the '\n' and therefore the macro does not end with it), Further, notice that we do not have a ';' after the makeClass(...) line. This prevents an error, as in a class definition, a ';' character sometimes behaves badly, and considering that we are inside a class definition (remember, the macro is substituted EXACTLY into the place where the macro is called), we don't want these. Further notice that we no longer need the braces, as the braces are now partially in the macro.

Now on to more interesting tricks... We can also begin to make C++ just like pascal... Observe:

#define begin {

#define end }

#define procedure void

We could continue, and though there is a stopping point, we already have the beginnings of a pascal system...

procedure main()

begin

if (x > 5)

begin

y = 1;

end

end

Isn't that icky....

undef

include

The short story is that whatever is in the file #include'd is substituted exactly where the include directive is placed. Further, the syntax of the #include command is the directive, and then the file in double quotes ('"', '"'), if it is in the same directory, and in angle brackets ('<', '>') if it is in the include path. Typically, this is not really paid attention to by the compiler, though it is convention that all include files that you write should be included using the double quotes, and any include files that you get from somewhere (usually the system) should be included using angle brackets. Further, the naming convention as of recently has been to include C++ include files without the .h extension and C files with the .h extension.

Example:

C++ program:

#include

#include "myClass"

int main() {

return 0;

}

C program:

#include

#include "myClass.h"

int main() {

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Some of them are:

#include

#define

#if, #else, endif, #elif, #ifdef, #ifndef

#line

#pragma

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

hello

what is the preprocessor in c++ for detect kind of cpu?

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Obviously the one you have not bothered to study. I would hate to be in your shoes when it is time to sit exams.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the different types of c preprocessor directives?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

In C plus plus what sign do all preprocessor directives start with?

all preprocessor directives start with #(hash) symbol in both c &amp; c++


What are the functions of a c processor?

The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).


What are preprocessor directives in c programing language?

Preprocessor directives are instructions to the preprocessor which modify the source code prior to compilation. The compiler never sees the directives, it only sees the modified source code. Preprocessor directives can be used to insert the contents of one file into another (#include), define or undefine a macro (#define, #undef), provide conditional compilation (#if, #ifdef, #ifndef, #else, #endif) or provide some implementation-defined operation (#pragma). When the preprocessor acts upon a directive, the directive is not included in the modified file. Where a directive defines a macro, all occurrences of the macro name within the source code are expanded according to the definition. Given that the compiler never sees that definition, this can lead to some obscure error messages where macro expansion results in a compile-time error.


When do preprocessor directives execute in c plus plus?

Preprocessing is the first stage of compilation, where macros are expanded, conditional compilation established and code replaced according to the specified directives. The resulting code produces intermediate source files which are then compiled by the main compilation process. Your IDE may include options to retain these intermediate files so you may examine them.


How do you implement c program for conditional compilation?

Conditional compilation is achieve through preprocessor directives. First, define the preprocessor symbols upon which conditional compilation depends, then test them using #if and #else preprocessor directives. A #endif directive indicates the end of the nearest enclosing conditional block, thus conditional blocks may be nested. The following example demonstrates how we can conditionally define debug or production code based upon the absence or existence of the NDEBUG symbol: #ifdef NDEBUG /* all C code within this block is compiled when NDEBUG is defined (production code) */ #else /* all C code within this block is compiled when NDEBUG is not defined (debug code) */ #endif Note that the NDEBUG symbol is typically defined via the command line, however symbols can also be defined or undefined via the source using the #define and #undefine directives. For instance, header files typically require guards to protect against being included more than once in a compilation and preprocessor directives provide the conventional means of ensuring that is the case: // myheader.h #ifndef _MYHEADER_H_ #define _MYHEADER_H_ // all header code goes here... #endif By convention, preprocessing symbols (macros) are defined with all uppercase and are intentionally ugly to avoid any confusion with C identifiers. Header guards must be unique to each header thus they are typically based upon the header file name itself.

Related questions

In C plus plus what sign do all preprocessor directives start with?

all preprocessor directives start with #(hash) symbol in both c &amp; c++


What is directives in c?

It's either the preprocessor instructions (#include #define etc), or the pragmas.


What are the various preprocessor directives available in c?

Some of them are: #include #define #if, #else, endif, #elif, #ifdef, #ifndef #line #pragma


What are the functions of a c processor?

The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).


What are preprocessor directives in c programing language?

Preprocessor directives are instructions to the preprocessor which modify the source code prior to compilation. The compiler never sees the directives, it only sees the modified source code. Preprocessor directives can be used to insert the contents of one file into another (#include), define or undefine a macro (#define, #undef), provide conditional compilation (#if, #ifdef, #ifndef, #else, #endif) or provide some implementation-defined operation (#pragma). When the preprocessor acts upon a directive, the directive is not included in the modified file. Where a directive defines a macro, all occurrences of the macro name within the source code are expanded according to the definition. Given that the compiler never sees that definition, this can lead to some obscure error messages where macro expansion results in a compile-time error.


When do preprocessor directives execute in c plus plus?

Preprocessing is the first stage of compilation, where macros are expanded, conditional compilation established and code replaced according to the specified directives. The resulting code produces intermediate source files which are then compiled by the main compilation process. Your IDE may include options to retain these intermediate files so you may examine them.


How does c plus plus code compiled?

When you invoke the compiler, the preprocessor (also known as the precompiler) runs first to process all the precompiler directives and macros; the lines beginning with the pound symbol (#) such as #define and #include. The preprocessor also strips out all of the comments. The preprocessor achieves this by creating one or more temporary, intermediate files containing nothing but C++ code. The compiler then compiles these intermediate files into object code which the linker uses to create the final executable.


What is difference between using declarative method and using directive method?

The simple answer is that directives are symbols that instruct or direct the preprocessor, while declarations are C++ symbols that are processed by the C++ compiler. The preprocessor and the compiler are essentially two completely separate operations. The preprocessor is tasked with modifying your source files in order to produce temporary intermediate source files which the compiler can process more efficiently. These intermediate files contain nothing but C++ code, thus the preprocessor's primary job is to strip out all the comments from your source files. Preprocessor directives are used to instruct the preprocessor in all other modifications, such as where and when to include one file within another and which sections of code are valid for a particular build. This is achieved by #define directives, in conjunction with #ifdef, #else and #endif directives. The #define directive defines a symbol. As the preprocessor encounters these directives, the symbols are placed in a definition table, which includes all the symbols that were initially passed via the command line. Some symbols have no value assigned to them; their presence in the table is sufficient to know they are defined, and the preprocessor can simply act according to whether a symbol is currently defined or not. The #undef directive is the antidote to #define, allowing symbols to be undefined (removed from the table) by the preprocessor. However, #define can also be used to assign a value to a symbol. This value acts a text replacement for the symbol, thus wherever the symbol is encountered within your code (outwith any other directives), the symbol is replaced with the text value associated with that symbol. These are known as macros. #define can also be used to declare macro functions, where the text replacement includes one or more arguments within parenthesis, followed by C++ code that processes those arguments. Thus all occurrences of the symbol in your code must include an argument list with the same number of arguments as the macro directive. The preprocessor then inline expands the macro function, replacing its arguments with the formal arguments provided by your code. The problem with #define in general is that it is not a C++ statement, it is a macro, and macros are not subject to the strong-typing that is fundamental to C++. So while you can use #define to declare literal constants, those constants are untyped. Moreover, since the compiler never sees the macro definition (it is stripped from the intermediate file), any errors that occur as a result of a macro expansion cannot be sourced back to the macro, which makes debugging difficult at best. In the case of macro functions, the debugger cannot help you at all, you're completely on your own. Thus when you have the option to use directives or declarations, always go with a declaration. C++ code is type safe, can be easily debugged, is easier to maintain, and usually results in much smaller compiled code. Only use the preprocessor to generate code that would be difficult or impossible to generate with the compiler alone. In other words, use directives specifically to direct the preprocessor. Never rely on them to generate C++ code unless you have no other option, or it would greatly simplify a far more complex C++ implementation.


What is a role of co-preprocessor in c?

Co-processor and pre-processor are two different thing. Pick one.


How do you implement c program for conditional compilation?

Conditional compilation is achieve through preprocessor directives. First, define the preprocessor symbols upon which conditional compilation depends, then test them using #if and #else preprocessor directives. A #endif directive indicates the end of the nearest enclosing conditional block, thus conditional blocks may be nested. The following example demonstrates how we can conditionally define debug or production code based upon the absence or existence of the NDEBUG symbol: #ifdef NDEBUG /* all C code within this block is compiled when NDEBUG is defined (production code) */ #else /* all C code within this block is compiled when NDEBUG is not defined (debug code) */ #endif Note that the NDEBUG symbol is typically defined via the command line, however symbols can also be defined or undefined via the source using the #define and #undefine directives. For instance, header files typically require guards to protect against being included more than once in a compilation and preprocessor directives provide the conventional means of ensuring that is the case: // myheader.h #ifndef _MYHEADER_H_ #define _MYHEADER_H_ // all header code goes here... #endif By convention, preprocessing symbols (macros) are defined with all uppercase and are intentionally ugly to avoid any confusion with C identifiers. Header guards must be unique to each header thus they are typically based upon the header file name itself.


What is preprocessor derective in C language?

#if, #define, #include just to name a few


Is C plus plus preprocessor software?

Sometimes, it is. Some implementations compile C++ code into C code, and then compile the C code.