Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to C Programming
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
13 C Preprocessor.
CSCE 206 Structured Programming in C
Chapter 4 – C Program Control
INC 161 , CPE 100 Computer Programming
Chapter 2 - Introduction to C Programming
INC 161 , CPE 100 Computer Programming
Chapter 2, Part I Introduction to C Programming
Chapter 13 - The Preprocessor
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
14. THE PREPROCESSOR.
Chapter 2 - Introduction to C Programming
Pre-processor Directives
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
C Preprocessor(CPP).
Programming in C Miscellaneous Topics.
Chapter 2 - Introduction to C Programming
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Conditional Compilation
Presentation transcript:

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing Directives Outline Introduction Symbolic Constants and Macros Source File Inclusion Conditional Compilation Predefined Macros A Sample Application Problem Pragma Directive NULL Directive Converting Token to Strings Token Merging in Macro Expansions Line Control Error Directive Predefined Macros in Ch

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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));

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach #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 */

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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. 1)#include –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.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Applications –Loading header files #include –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)

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Conditional Compilation 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

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Preprocessing directives of the forms #if expr1 /*... */ #elif expr2 /*... */ #else /*... */ #endif check whether the controlling expression evaluates to nonzero. Every #if ends with #endif Example: #if defined(_HPUX_) printf(“I am using HP-UX\n”); #elif defined(_WIN32_) printf(“I am using Windows\n); #endif

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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) 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.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach 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

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The nomenclature related to the modeling of the system.is listed below. m -- mass of the body x -- position of the body v -- velocity of the body a -- acceleration of the body g -- gravitational acceleration  -- friction coefficient f -- friction force N -- normal force Equation of motion: The equation of the motion of the system can be derived based on the Newton's second law. N = mg (1) f =  N (2) p-f = ma (3) From equation (1), (2) and (3), the formula for calculating the acceleration of the rigid body can be derived as follows. a = (p-  mg)/m (4)

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Problem Statement: For the system shown in Figure1(a), given m = 5 kg, g = 9.81 m/s 2,  = 0.2. The external force p is expressed as a function of time t, p(t) = 4(sin(t)-3)+20 when t >= 0 write a program to calculate the acceleration a when t = 2 seconds. (1)Solve acceleration using macros for formulas of force p and acceleration a. (2)Solve acceleration using functions for force p and acceleration. The function prototypes and macros shall be defined in header file accel.h.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Program 1 : /* File: accelmacro.c */ #include #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 = (m/s^2)

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach /* 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 /* File: accelhead.c */ #include /* local header file */ #include "accel.h" int main() { /* declare variables */ double a, mu, m, t; /* 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; } Program 2:

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Predefined Macros Macros defined in C

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Example: /* filename: predefined.c */ #include int main() { printf("__FILE__ = %s\n", __FILE__); printf("__LINE__ = %d\n", __LINE__); printf("__DATE__ = %s\n", __DATE__); printf("__TIME__ = %s\n", __TIME__); #ifdef __STDC__ printf("__STDC__ = %d\n", __STDC__); #endif #ifdef __STDC_VERSION___ printf("__STDC_VERSION__ = %d\n", __STDC_VERSION__); #endif return 0; }

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Output: __FILE__ = predefined.c __LINE__ = 6 __DATE__ = Feb __TIME__ = 00:09:42 __STDC__ = 1 __STDC_VERSION_ =

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach /* File: printerror.c */ #include #define printerror() printf("Error in %s:%s():%d\n", \ __FILE__, __func__, __LINE__); void funcname1() { printerror(); } void funcname2() { printerror(); } int main() { funcname1(); funcname2(); return 0; } Example Error in printerror.c:funcname1():7 Error in printerror.c:funcname2():10 Output

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Pragma Directive A preprocessing directive of the form #pragma pp-tokens-opt new-line is called a pragma directive. It is compiler/interpreter dependent. Some preprocessing token names for the pragma directive in Ch are defined below.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach ‡ Slides for optional topics in C

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The following form #include pp-tokens is also permitted. The preprocessing tokens after #include in the directive are processed just as normal text.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach NULL Directive A preprocessing directive of the form #new-line has no effect on the program. The line is ignored.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The # token appearing within a macro definition is recognized as a unary stringization operator. In a replacement list, if a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument. Example: > #define TEST(a) #a > printf(“%s”, TEST(abcd)) abcd –The macro parameter abcd has been converted to the string constant “abcd”. It is equivalent to > printf(“%s”, “abcd”) Converting Token to Strings

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Each occurrence of white space between the argument’s preprocessing tokens becomes a single space character in the character string literal. White space before the first preprocessing token and after the last preprocessing token composing the argument is deleted. Example: > #define TEST(a) #a > printf(“1%s2”, TEST( a b )) 1a b2 –The argument is turned into the string constant “a b”.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Token Merging in Macro Expansions The merging of tokens to form new tokens in C is controlled by the presence of the merging operator ## in macro definitions. The common use of concatenation is concatenating two names into a longer name. It is possible to concatenate two numbers, or a number and a name, such as ‘1.5’ and ‘e3’, into a number. Example: > #define CONC2(a, b) a ## b > #define CONC3(a, b, c) a ## b ## c > CONC2(1, 2) 12// numbers ‘1’ and ‘2’ concatenated to ‘12’ > CONC3(3, +, 4) 7// ‘3’, ‘+’, and ‘4’ becomes ‘3+4’, which equals ‘7’ > printf(“CONC2(1,2) = %d”, CONC2(1,2)) CONC2(1,2) = 12

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Line Control The #line directive can be used to alter the line numbers assigned to the source code. This directive gives a new line number to the following line, which is then incremented to derive the line number for subsequent lines. A preprocessing directive of the form #line digit-sequence new-line causes the implementation to behave as if the following sequence of source lines begins with a source line that has a line number as specified by the digit sequence. A preprocessing directive of the form #line digit-sequence “s-char-sequence-opt” new-line sets the presumed line number similarly and changes the presumed name of the source file to be the contents of the character string literal. The name of the source file is stored in the predefined macro __FILE__ internally.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Example: /* FILENAME: linefile.c */ #include int main() { printf(“before line directive, line number is %d \n”, __LINE__); printf(“the FILE predefined macro = %s\n”, __FILE__); #line 200 “newname” printf(“after line directive, line number is %d \n”, __LINE__); printf(“The FILE predefined macro = %s\n”, __FILE__); return 0; } Output: before line directive, line number is 4 the FILE predefined macro = linefile.c after line directive, line number is 200 the FILE predefined macro = newname

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach A preprocessing directive of the form #error pp-tokens-opt new-line causes the implementation to produce a diagnostic message that includes the specified sequence of preprocessing tokens and the interpretation to cease. Example: /* FILENAME: error.c */ #include #define MACRO int main() { #ifdef MACRO #error This is an error, if your code reach here /* the code here will not be processed */ #else printf("ok \n"); #endif return 0; } Error Directive

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Output: ERROR: #error: This is an error, if your code reach here ERROR: syntax error before or at line 6 in file ’error.c' ==>: #error This is an error, if your code reach here BUG: #error This is an error, if your code reach here<== ??? WARNING: cannot execute command ‘error.c’

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach ‡ Slides for optional topics in Ch

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Predefined Macros in Ch

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Platform-dependent predefined macros in Ch