13 C Preprocessor.

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Files in C Rohit Khokher.
Pemrograman C Risanuri Hidayat. Why C  Compact, fast, and powerful  “Mid-level” Language  Standard for program development (wide acceptance)  It is.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 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
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.
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)
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Syntax and Semantics, and the Program Development Process.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
C Hints and Tips The preprocessor and other fun toys.
Programming Practice (6) - Sorting Algorithms
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
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 7: Preprocessing.
 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.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
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.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
© 2016 Pearson Education, Ltd. All rights reserved.
INC 161 , CPE 100 Computer Programming
Chapter 2, Part I Introduction to C Programming
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
ICS103 Programming in C Lecture 3: Introduction to C (2)
Functions Separate Compilation
14. THE PREPROCESSOR.
A First Book of ANSI C Fourth Edition
Pre-processor Directives
7 Arrays.
Introduction to C++ Programming
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Introduction to C++ Programming
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Lab guide # 12 Review C Preprocessor And Q&A.
C Programming Language
What Is? function predefined, programmer-defined
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

13 C Preprocessor

Hold thou the good; define it well. Alfred, Lord Tennyson I have found you an argument; but I am not obliged to find you an understanding. Samuel Johnson A good symbol is the best argument, and is a missionary to persuade thousands. Ralph Waldo Emerson

Conditions are fundamentally sound. Herbert Hoover [December 1929] The partisan, when he is engaged in a dispute, cares nothing about the rights of the question, but is anxious only to convince his hearers of his own assertions. Plato

OBJECTIVES In this chapter you will learn: To use #include for developing large programs. To use #define to create macros and macros with arguments. To use conditional compilation to specify portions of a program that should not always be compiled (such as code that assists you in debugging). To display error messages during conditional compilation. To use assertions to test if the values of expressions are correct.

13.1 Introduction 13.2 #include Preprocessor Directive 13.3 #define Preprocessor Directive: Symbolic Constants 13.4 #define Preprocessor Directive: Macros 13.5 Conditional Compilation 13.6 #error and #pragma Preprocessor Directives 13.7 # and ## Operators 13.8 Line Numbers 13.9 Predefined Symbolic Constants 13.10 Assertions

13.1 Introduction Preprocessing Format of preprocessor directives Occurs before a program is compiled Inclusion of other files Definition of symbolic constants and macros Conditional compilation of program code Conditional execution of preprocessor directives Format of preprocessor directives Lines begin with # Only whitespace characters before directives on a line

13.2 The #include Preprocessor Directive Copy of a specified file included in place of the directive #include <filename> Searches standard library for file Use for standard library files #include "filename" Searches current directory, then standard library Use for user-defined files Used for: Programs with multiple source files to be compiled together Header file – has common declarations and definitions (classes, structures, function prototypes) #include statement in each file

13.3 The #define Preprocessor Directive: Symbolic Constants Preprocessor directive used to create symbolic constants and macros Symbolic constants When program compiled, all occurrences of symbolic constant replaced with replacement text Format #define identifier replacement-text Example: #define PI 3.14159 Everything to right of identifier replaces text #define PI = 3.14159 Replaces “PI” with "= 3.14159" Cannot redefine symbolic constants once they have been created

Good Programming Practice 13.1 Using meaningful names for symbolic constants helps make programs more self-documenting.

Good Programming Practice 13.2 By convention, symbolic constants are defined using only uppercase letters and underscores.

13.4 The #define Preprocessor Directive: Macros 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 ) );

13.4 The #define Preprocessor Directive: Macros Use parentheses Without them the macro #define CIRCLE_AREA( x ) PI * ( x ) * ( x ) would cause area = CIRCLE_AREA( c + 2 ); to become area = 3.14159 * c + 2 * c + 2; Multiple arguments #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) rectArea = RECTANGLE_AREA( a + 4, b + 7 ); rectArea = ( ( a + 4 ) * ( b + 7 ) );

Common Programming Error 13.1 Forgetting to enclose macro arguments in parentheses in the replacement text can lead to logic errors.

Performance Tip 13.1 Macros can sometimes be used to replace a function call with inline code prior to execution time. This eliminates the overhead of a function call. Today’s optimizing compilers will often inline functions for you, so many programmers no longer use macros for this purpose.

13.4 The #define Preprocessor Directive: Macros #undef Undefines a symbolic constant or macro If a symbolic constant or macro has been undefined it can later be redefined

13.5 Conditional Compilation Control preprocessor directives and compilation Cast expressions, sizeof, enumeration constants cannot be evaluated in preprocessor directives Structure similar to if #if !defined( NULL ) #define NULL 0 #endif Determines if symbolic constant NULL has been defined If NULL is defined, defined( NULL ) evaluates to 1 If NULL is not defined, this function defines NULL to be 0 Every #if must end with #endif #ifdef short for #if defined( name ) #ifndef short for #if !defined( name )

13.5 Conditional Compilation Other statements #elif – equivalent of else if in an if statement #else – equivalent of else in an if statement "Comment out" code Cannot use /* ... */ Use #if 0 code commented out #endif To enable code, change 0 to 1

13.5 Conditional Compilation Debugging #define DEBUG 1 #ifdef DEBUG cerr << "Variable x = " << x << endl; #endif Defining DEBUG to 1 enables code After code corrected, remove #define statement Debugging statements are now ignored

Common Programming Error 13.2 Inserting conditionally compiled printf statements for debugging purposes in locations where C currently expects a single statement. In this case, the conditionally compiled statement should be enclosed in a compound statement. Thus, when the program is compiled with debugging statements, the flow of control of the program is not altered.

13.6 The #error and #pragma Preprocessor Directives #error tokens Tokens are sequences of characters separated by spaces "I like C++" has 3 tokens Displays a message including the specified tokens as an error message Stops preprocessing and prevents program compilation #pragma tokens Implementation defined action (consult compiler documentation) Pragmas not recognized by compiler are ignored

13.7 The # and ## Operators # Causes a replacement text token to be converted to a string surrounded by quotes The statement #define HELLO( x ) printf( “Hello, ” #x “\n” ); would cause HELLO( John ) to become printf( “Hello, ” “John” “\n” ); Strings separated by whitespace are concatenated when using printf

13.7 The # and ## Operators ## Concatenates two tokens The statement #define TOKENCONCAT( x, y ) x ## y would cause TOKENCONCAT( O, K ) to become OK

13.8 Line Numbers #line Renumbers subsequent code lines, starting with integer value File name can be included #line 100 "myFile.c" Lines are numbered from 100 beginning with next source code file Compiler messages will think that the error occurred in "myfile.C" Makes errors more meaningful Line numbers do not appear in source file

13.9 Predefined Symbolic Constants Four predefined symbolic constants Cannot be used in #define or #undef

Fig. 13.1 | Some predefined symbolic constants.

13.10 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

Software Engineering Observation 13.1 Assertions are not meant as a substitute for error handling during normal runtime conditions. Their use should be limited to finding logic errors.