Presentation is loading. Please wait.

Presentation is loading. Please wait.

INC 161 , CPE 100 Computer Programming

Similar presentations


Presentation on theme: "INC 161 , CPE 100 Computer Programming"— Presentation transcript:

1 INC 161 , CPE 100 Computer Programming
Lecture 9 Preprocessing

2 Introduction Preprocessing Format of preprocessing directives
Preprocessing = Do before translating to a machine code 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. # is for preprocessing directives (macro)

3 Note: These commands are not changed to machine code.

4 Code with preprocessing
Compile Procedure Code with preprocessing Substitute code Cut code C Code Change to Machine Language Machine Language

5 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.

6 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

7 Code with preprocessing
#define BLOCK 32 main() { int i,j; i = BLOCK; j = BLOCK + 4; } main() { int i,j; i = 32; j = ; }

8 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));

9 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.

10 #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 */

11 Source File Inclusion The #include Preprocessing Directive
Copy of a specified header file included in place of the directive. It has following two common forms. #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 #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>.

12 #include <stdio.h> #include “add.h” main() { …. add(1,2);
test.c add.h total #include <stdio.h> #include “add.h” main() { …. add(1,2); printf(“test”) } #include <stdio.h> int add(int x, int y) { …. printf(“add”) } main() add(1,2); printf(“test”) int add(int x, int y) { …. printf(“add”) }

13 Applications Loading header files #include <stdio.h>
Programs with multiple source files to be compiled together Includes user defined header files which have common declarations and definitions (classes, structures, function prototypes, and macros)

14 Conditional Compilation
Enables the user to control the compilation of the program, screen out portions of source code that are not to be compiled. Structure The structure is similar to if and else statement in C. Conditional preprocessing directives #if, #else, #elif, and #endif

15 Preprocessing directives of the forms
#if expr1 /* ... */ #elif expr2 #else #endif check whether the controlling expression evaluates to nonzero. Every #if ends with #endif #define HELP_MODE : #if defined(HELP_MODE) printf(“This is help”); #endif printf(“This is help”); Preprocessed

16 Preprocessing directives of the forms # ifndef identifier
# 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 #if !defined(identifier) Each directive’s condition is checked in order. If it evaluates to false (zero), then the group that it controls is skipped: directives are processed only through the name that determines the directive in order to keep track of the level of nested conditionals. Only the first group whose control condition evaluates to true (nonzero) is processed. If none of the conditions evaluates to true, and there is a #else directive, then the group controlled by the #else is processed; if lacking a #else directive, then all the groups until the #endif are skipped.

17 Debugging code #define DEBUG
/* ... */ double x; x = some_func(); #ifdef DEBUG printf(“The value of x = %f\n”, x); #endif Defining DEBUG to print out the value of x. After debugging, remove #define statement. The debugging statements are ignored.

18 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 /* code */ #endif

19 Multiple placement of include
2 includes Multiple placement of include #include <stdio.h> main() { } #ifndef HEADER_H #define HEADER_H /* code */ #endif main() { } #define HEADER_H /* code */ #ifndef HEADER_H #endif main() { }

20 /* code */ only paste once #define HEADER_H /* code */
#ifndef HEADER_H #endif main() { } #define HEADER_H /* code */ main() { } /* code */ only paste once

21 Program 1: Output: Acceleration = 1.364823 (m/s^2)
/* File: accelmacro.c */ #include <stdio.h> #define M_G #define FORCE(t) (4*(t)-3)+20 #define ACCEL(p, mu, m) (((p)-(mu)*(m)*M_G)/(m)) 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); } Output: Acceleration = (m/s^2)


Download ppt "INC 161 , CPE 100 Computer Programming"

Similar presentations


Ads by Google