Download presentation
Presentation is loading. Please wait.
Published byOpal Jones Modified over 9 years ago
1
Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training
2
Training C/C++ EcoSoftware 2 The Preprocessor o Introduction preprocessor. o Preprocessing o Logical Preprocessor Directives o Standard Preprocessing Macros o Error Generation o Using the assert() Macro
3
Training C/C++ EcoSoftware 3 Introduction preprocessor o The preprocessor is powerful tool. o It also can be a source of hard-to-find bugs. o It can be easily be misused to create program that are almost impossible to understand. o The preprocessor is controlled by processing directive that begin a # character. Modified C program C Program Object code
4
Training C/C++ EcoSoftware 4 Preprocessing o Including Header Files A header file is any external file whose contents It use of the #include preprocessor directive. Syntax : #include o External Variables and Functions A program that’s made up of several source files want to use a global variable that’s defined in another file. Declaring the variable as external to the current file using the extern keyword. Syntax : extern datatype namevariable; Example : extern int number;
5
Training C/C++ EcoSoftware 5 Preprocessing o Macro definition. The #define directive defines a macro. The #undefine directive remove a macro definition. Syntax : #define identifer replacement-list Example : #define PI 3.14159265 #define Black White Advance macro definition. It make program easier to read. It make program easier to modify It help avoid inconsistencies and topographical error. –Example : Numerical constant like : 3.14149, but some time 3.1415
6
Training C/C++ EcoSoftware 6 Preprocessing o Macro definition. Advance macro definition Making minor changes to syntax of C program. –Example : #define BEGIN { –#define END } Renaming types. –Exampe : #define BOOL int Controlling conditional compilation. –Example : #define DEBUG
7
Training C/C++ EcoSoftware 7 Preprocessing o Macros That Look Like Functions Allows parameters to be specified, which may themselves be replaced by argument values, Syntax : #define identifer(x1,x2,..,xn) replacement list Example : #define max(x, y) x>y ? x : y Call : result = myval>99 ? myval : 99; It mean : result = max(myval, 99);
8
Training C/C++ EcoSoftware 8 Preprocessing o Preprocessor Directives on Multiple Lines using the statement continuation character, \. Example : #define min(x, y) \ ((x)<(y) ? (x) : (y)) o Strings As Macro Arguments Example: #define MYSTR "This string" Call : printf("%s", MYSTR);
9
Training C/C++ EcoSoftware 9 Preprocessing o Joining Two Results of a Macro Expansion you may wish to generate two results in a macro and join them together Using two characters ## serves to separate the parameters. Syntax : #define join(a, b) a##b Example : strlen(join(var, 123)); It will be result : strlen(var123);
10
Training C/C++ EcoSoftware 10 Logical Preprocessor Directives o Conditional Compilation Syntax : #if defined identifier #endif Or #if !defined identifier #endif Or #if !defined block1 #define block1 /* Block of code you do not */ /* want to be repeated. */ #endif
11
Training C/C++ EcoSoftware 11 Logical Preprocessor Directives o Directives Testing for Specific Values Using #if directive to test the value of a constant expression. Syntax : #if constant_expression #endif Example : #if CPU == Pentium4 printf("\nPerformance should be good." ); #endif
12
Training C/C++ EcoSoftware 12 Logical Preprocessor Directives o Multiple-Choice Selections Using #if, #elseif, #else derective for excecute statements. Syntax : #if constant_expression #elseif constant_expression #else #endif Example : #if CPU == Pentium4 printf("\nPerformance should be good." ); #else printf("\nPerformance may not be so good." ); #endif
13
Training C/C++ EcoSoftware 13 Standard Preprocessing Macros o Using __DATE__ macro provides a string representation of the date. o Using __TIME__, provides a string containing the value of the time. o Using __FILE__, A string literal containing the name of the file being compiled. o __LINE__ A decimal constant containing the current source line number. o __STDC_VERSION__ : This macro expands to the C Standard's version number. o Example : printf("\nProgram last compiled at %s on %s", __TIME__, __DATE__ );
14
Error Generation o A preprocessor error directive causes the preprocessor to generate an error message and causes the compilation to fail. o Syntax : # error token-sequence o Example : #define BUFFER_SIZE 255 #if BUFFER_SIZE < 256 #error "BUFFER_SIZE is too small." #endif generates the error message: BUFFER_SIZE is too small. Training C/C++ EcoSoftware 14
15
Using the assert() Macro o The assert() macro is defined in the standard library header file. o Using #ndefine derectives for Switch on or off assertions. Syntax: #undef NDEBUG #include Example: Training C/C++ EcoSoftware 15 #undef NDEBUG #include int main(void) { int y = 5; for(int x = 0 ; x < 20 ; x++) { printf("\nx = %d y = %d", x, y); assert(x<y); } return 0; }
16
Training C/C++ EcoSoftware 16 Thank You End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.