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.
all preprocessor directives start with #(hash) symbol in both c & c++
The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).
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.
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.
#if, #define, #include just to name a few
all preprocessor directives start with #(hash) symbol in both c & c++
It's either the preprocessor instructions (#include #define etc), or the pragmas.
Some of them are: #include #define #if, #else, endif, #elif, #ifdef, #ifndef #line #pragma
The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if).
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.
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.
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.
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.
Co-processor and pre-processor are two different thing. Pick one.
#if, #define, #include just to name a few
Sometimes, it is. Some implementations compile C++ code into C code, and then compile the C code.
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.