Lab guide # 12 Review C Preprocessor And Q&A
Key points #include Preprocessor Directive #define Preprocessor Directive: Symbolic Constants #define Preprocessor Directive: Macros #error and #pragma Preprocessor Directives # and ## Operators Line Numbers Predefined Symbolic Constants Assertions
For example in “constants.h” we have #ifndef _CONSTANT_ #define _CONSTANT_ #define pi 3.14159265358979 #define h 1.05459e-34 /* Planck's constant/2PI in J-sec */ #define am0 9.10953e-31 /* Electron rest mass in kg */ #define q 1.60219e-19 /* Electron charge */ #define kb 1.3806505e-23 /* Boltzmann constant J/Kelvin */ #define eps_0 8.85419e-12 /* Permittivity of free space. unit F/m */ #define max_electron_number 370000 #endif //_CONSTANT_
Macro Operation defined in #define A macro without arguments is treated like a symbolic constant A macro with arguments has its arguments substituted for replacement text, when the macro is expanded Performs a text substitution – no data type checking The macro #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) would cause area = CIRCLE_AREA( 4 ); to become area = ( 3.14159 * ( 4 ) * ( 4 ) );
Assertions assert macro Header <assert.h> Tests value of an expression If 0 (false) prints error message and calls abort Example: assert( x <= 10 ); If NDEBUG is defined All subsequent assert statements ignored #define NDEBUG
Review chapter 7-12 for Final Exam C pointers C characters and Strings C formatted Input/Output C Structures, Unions, Bit manipulations and Enumerations C File processing C Data structures
Q&A About HW and Lab assignments