Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Preprocessor(CPP).

Similar presentations


Presentation on theme: "C Preprocessor(CPP)."— Presentation transcript:

1 C Preprocessor(CPP)

2 CPP The C preprocessor (cpp) is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if) Preprocessor directives are lines included in the code of our programs that are not program statements but directives for the preprocessor. These lines are always preceded by a hash sign (#)

3 Facilities provided The C preprocessor provides four separate facilities: Inclusion of header files. These are files of declarations that can be substituted into your program. Macro expansion. You can define macros. Conditional compilation. You can include or exclude parts of the program according to various conditions. Line control. If you use a program to combine or rearrange source files into an intermediate file which is then compiled, you can use line control to inform the compiler of where each source line originally came from.

4 The #include Preprocessor Directive
#include directive Puts copy of file in place of directive Seen many times in example code Two forms #include <filename> For standard library header files Searches predesignated directories #include "filename" Searches in current directory Normally used for programmer-defined files

5 Cont.. Usage Loading header files Programs with multiple source files
#include <iostream> Programs with multiple source files Header file Has common declarations and definitions Classes, structures, enumerations, function prototypes Extract commonality of multiple program files

6 The #define Preprocessor Directive: Symbolic Constants
Constants represented as symbols When program compiled, all occurrences replaced Format #define identifier replacement-text #define PI Everything to right of identifier replaces text #define PI= Replaces PI with "= " Probably an error Cannot redefine symbolic constants

7 Cont.. Advantages Disadvantages const variables preferred
Takes no memory Disadvantages Name not be seen by debugger (only replacement text) Do not have specific data type const variables preferred

8 The #define Preprocessor Directive: Macros
A macro is a sort of abbreviation which you can define once and then use later. Simple Macros It is a name which stands for a fragment of code. Before you can use a macro, you must define it explicitly with the `#define' directive. `#define' is followed by the name of the macro and then the code it should be an abbreviation for. For example,  #define BUFFER_SIZE defines a macro named `BUFFER_SIZE' as an abbreviation for the text `1020'. Eg:  foo = (char *) xmalloc (BUFFER_SIZE); then the C preprocessor will recognize and expand the macro `BUFFER_SIZE', resulting in foo = (char *) xmalloc (1020);

9 Conditional Compilation
A conditional in the C preprocessor begins with a conditional directive: `#if', `#ifdef' or `#ifndef'. The `#if' Directive - Basic conditionals using `#if' and `#endif'. The `#else' Directive - Including some text if the condition fails. The `#elif' Directive - Testing several alternative possibilities.

10 Forms of directives The `#if' Directive #if expression controlled text
#endif /* expression */ The `#else' Directive The `#else' directive can be added to a conditional to provide alternative text to be used if the condition is false.   #if expression text-if-true #else /* Not expression */ text-if-false #endif /* Not expression */

11 The `#error‘ Directives
The directive `#error' causes the preprocessor to report a fatal error. The rest of the line that follows `#error' is used as the error message. The line must consist of complete tokens. Tokens are groups of characters separated by spaces Eg: #error 1 - Out of range error

12 The `#warning‘ directive
The directive `#warning' is like the directive `#error', but causes the preprocessor to issue a warning and continue preprocessing. The rest of the line that follows `#warning' is used as the warning message.

13 Line Numbers One of the jobs of the C preprocessor is to inform the C compiler of where each line of C code came from: which source file and which line number. `#line' is a directive that specifies the original line number and source file name for subsequent input in the current preprocessor input file.  `#line' has three variants: #line linenum #line linenum filename #line anything else - anything else is checked for macro calls, which are expanded. The result should be a decimal integer constant

14 Assertions Assertions are a more systematic alternative to macros in writing conditionals to test what sort of computer or system the compiled program will run on. Assertions are usually predefined, but you can define them with preprocessing directives or command-line options.


Download ppt "C Preprocessor(CPP)."

Similar presentations


Ads by Google