Chapter 7: Preprocessing Directives
Introduction Preprocessing Affect program preprocessing and execution Capabilities Inclusion of additional C source files Definition of symbolic constants and macros Conditional preprocessing of a program Format of preprocessing directives A preprocessing directive consists of a sequence of preprocessing tokens that begins with a pound sign #, which must be the first non-space character on the line. Some preprocessing directives are listed below.
Directive #define #elif #else #endif #error Description Define a preprocessor macro. Alternatively include some text based on the value of another expression, if the previous #if, #ifdef, #ifndef, or #leif test failed. Alternatively include some text, if the previous #if, #ifdef, #ifndef, or #elif test failed. Terminate conditional text. Produce a compile-time error with a designated message. #if Conditionally include text, based on the value of an expression. #ifdef #ifndef #include #line #pragma Conditionally include text, based on whether a macro name is defined. Conditionally include text, based on if a name is not a defined macro. Insert text from another source file. Give a line number for message. Compiler/interpreter specific features, not in C standard # Null directive defined Preprocessing operator that yields 1 if a name is defined as a preprocessing macro and 0 otherwies; used in #if and #elif.
7.1 Symbolic Constants and Macros The #define Preprocessing Directive This preprocessing directive is used to create symbolic constants and macros. Form #define identifier replacement-list defines an object-like macro that causes each subsequent instance of the macro names to be replaced by the replacement-list of preprocessing tokens that constitute the remainder of the directive. The new-line is a character that terminates the #define preprocessing directive.
Symbolic constants The simple form of macro is particularly useful for introducing named constants into a program. It allows for easier modification of the constants later on.when programs are processed, all occurrences of symbolic constants indicated by identifier are replaced by the replacement-list. Example: #define BLOCK_SIZE 0x100 we can write int size = BLOCK_SIZE; instead of int size = 0x100; in the program. Note: Cannot redefine symbolic constants with different values by multiple #define statements
A preprocessing directive of the form #define identifier(identifier-list-opt) replacement-list new-line defines a function-like macro with arguments, similar syntactically to a function call. The parameters are specified by the optional list of identifiers. Example: if a macro mul with two arguments is defined by #define mul(x,y) ((x)*(y)) then the source program line result = mul(5, a+b); is replaced with result = ((5)*(a+b));
NOTE: Parentheses are important in macro definitions. Example: If macro mul is defined as #define mul(x,y) (x*y) The statement result = mul(5, a+b); in the source program becomes result = (5*a+b); The evaluation will be incorrect.
#undef Undefine a symbolic constant or macro, which can later be redefined. Example: #define mul(x,y) ((x)*(y)) /* */ #undef mul int mul; /* mul can be used after it is undefined */
We ve covered Section 7.1 Read and understand Program 7.1 Solve in notebook end-of-chapter problems 3 4 5
7.2 Predefined Macros See Table 7.2 on p.234 LINE FILE DATE TIME STDC STDC_VERSION Not defined in Visual Studio
7.2 Predefined Macros For the preprocessor, multi-line statements MUST be split with \ Hard-to-find syntax error!! This is func in C99, but in Visual Studio it must be spelled like this
7.3 Source File Inclusion The #include Preprocessing Directive 1)#include <header.h> Searches standard library for header file and replaces the directive by the entire contents of the file. In Ch, the header is searched according to the paths specified by the the system variable _ipath. C compilers in Unix will typically search the header file in the directory /usr/include. In Visual C++, the header file is searched based on the paths in the environment variable INCLUDE. or cl I C:/home/assount/include program.c Used for standard library files 2) #include "header.h" C compilers and interpreters will first search the header file in the same directory where the file is being processed, which typically is the current directory. Then search the header file in the paths as if it was included by #include <header.h>.
See programming example
7.4 Conditional Preprocessing and Compilation Enables the user to control the compilation of the program, screen out portions of source code that are not to be compiled. Structure is similar to if and else statement in C. Conditional preprocessing directives #if, #else, #elif, and #endif
Example: #if defined(_hpux_) printf( I am using HP-UX\n ); #elif defined(_win32_) printf( I am using Windows\n); #endif
Preprocessing directives of the forms # ifdef identifier # ifndef identifier check whether the identifier is or is not currently defined as a macro name. #ifdef identifier is the short form of #if defined(identifier) #ifndef identifier is the short form of #if!defined(identifier)
Comment out a segment of code Comment out code segment which contains /*... */ Use following format to comment out the segment of code double d = some_func(); #ifdef JUNK /* This code segment will be commented out */ printf( d = %f\n, d); #endif The code segment will be commented out when JUNK is not defined, To uncomment the code segment, define JUNK or remove #ifdef JUNK and #endif.
To include a header file in a program only once, it is typically handled using the combination of the following preprocessing directives #ifndef, #define, and #endif. For example, a header file header.h may consist of the following code fragment. #ifndef HEADER_H #define HEADER_H #endif
SKIP Sections 7.5, 7.6, 7.7, 7.8, 7.9
7.10 pragma directive Signals some platform-dependent functionality, e.g.
Sample Problem: The system in Figure1 (a) consists of a single body with mass m moving on a horizontal surface. An external force p acts on the body. The coefficient of kinetic friction between body and horizontal surface is. The freebody diagram for the system is shown in Figure1 (b). Figure1: The system diagram and FBD of a sample problem
Program 1: /* File: accelmacro.c */ #include <stdio.h> #define M_G 9.81 #define FORCE(t) (4*(sin(t)-3)+20) #define ACCEL(p, mu, m) (((p)-(mu)*(m)*m_g)/(m)) int main() { double a, p, mu, m, t; } mu = 0.2; m = 5.0; t = 2.0; p = FORCE(t); a = ACCEL(p, mu, m); // or a = ACCEL(FORCE(t), mu, m); printf("acceleration a = %f (m/s^2)\n", a); return 0; Output: Acceleration = 1.364823 (m/s^2)
Program 2: /* File: accelhead.c */ #include <stdio.h> /* local header file */ #include "accel.h" int main() { /* declare variables */ double a, mu, m, t; /* File: accel.h */ #ifndef ACCEL_H #define ACCEL_H #define M_G 9.81 double force(double t); double accel(double t, double mu, double m); #endif } /* Initialize variables */ mu = 0.2; m = 5.0; t = 2.0; /* processing */ a = accel(t, mu, m); /* display the output and termination */ printf("acceleration a = %f (m/s^2)\n", a); return 0; double force(double t) { double p; } p = 4*(sin(t)-3)+20; return p; double accel(double t, double mu, double m) { double a, p; } p = force(t); a = (p-mu*m*m_g)/m; return a;
Homework for Ch.7 Due Monday, Oct 25 End of chapter problems 9 10 14