Download presentation
Presentation is loading. Please wait.
1
CS 202 Computer Science II Lab Fall 2009 October 1
2
Today Topics Pre-Processor Directive
3
Pre-Processor Pre-Processor Directive : An instruction to the compiler to do something before compiling the source code, declared by using “#” symbol Pre-Processor : A program that handles these directive. It’s because the program has to do some processing before the compilation process starts. Pre-Processor Directive Location : – Usually at the beginning of the source file. – Also possible inside the source code.
4
Important Directive #include #define & #undef #if #endif #else #elif #ifdef #ifndef
5
#include Header File : An external file whose contents are included into program by using the “#include” Include unused library : Increase compilation time. #include – #include #include “user_file” – #include “my_header.h” – #include “my_file.cpp” Difference : With “ ” the compiler will look at the default header file directory, where as the other will look at the path specified under quotation mask
6
#define & #undef Substitutions In program #define identifier sequence_of_characters – #define array_size 100 #define identifier(list_of_identifiers) substitution_string – #define max(x, y) x>y ? x:y #undefine identifier : usually use with logical preprocessor directive – #undefine block1
7
Logical Preprocessor Directive Conditional Compilation – #if defined identifier – #endif Directive Testing for Specific Values – #if constant_expression Multiple Choice Selections – #if – #else or #elif – #endif
8
Conditional Compilation #if defined identifier or #ifdef // if specifics identifier is defined the code after // #if statement will be executed until #endif #endif #if !defined identifier or #ifndef #define identifier // block of code that you don’t want to repeat it // to avoid duplication of code #endif
9
test1 //test 1 //#define myEndl endl cout<<“no syntax error!!!!!"<<myEndl; //end test 1
10
test2 //test 2 int a; #define myCin cin>> #define myCout cout<< myCin a; myCout a; //end test 2
11
test3 #define testDef 25 cout<<"testDef: "<<testDef<<endl; #ifdef testDef #if testDef == 24 cout<<"not equal"<<endl; #elif testDef == 25 cout<<"equal"<<endl; #endif #undef testDef #ifndef testDef cout<<"not defined"<<endl; #else cout<<"defined"<<endl; #endif
12
test4 //test 4 #define myMax(a,b) (a>b?a:b) int x = 12, y = 23; cout<<myMax(x,y)<<endl; #undef myMax // cout<<myMax(x,y)<<endl; //end test 4
13
Directives Testing for Specific Values To test the value of a constant expression. If the value of the constant expression is not zero, the block of code will be executed until #endif #if CPU == Pentium cout << “Performance should be good.” << endl; #endif The above example will be included into your program only if you previously defined CPU with the value of Pentium
14
Multiple Choice Selections #if CPU == Pentium cout << “Performance should be good.” << endl; #else cout << “Performance might not be good.” << endl; #endif #if CPU == XEON_PIV cout << “Performance is very good.” << endl; #elif CPU == Pentium cout << “Performance should be good.” << endl; #else cout << “Performance might not be good.” << endl; #endif
15
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.