14. THE PREPROCESSOR.

Slides:



Advertisements
Similar presentations
THE PREPROCESSOR. Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Files in C Rohit Khokher.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
 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.
Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
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.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
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.
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 7: Preprocessing.
 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.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
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.
Chapter 14: The Preprocessor Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 14 The Preprocessor.
 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.
Chapter 14: The Preprocessor Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 14 The Preprocessor.
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.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
What Is? function predefined, programmer-defined
CSC201: Computer Programming
Announcements Midterm2 Grades to be announced THIS WEEK
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
Introduction to C Topics Compilation Using the gcc Compiler
Pre-processor Directives
Chapter 4 Macro Processors
Preprocessor C program → Modified C program → Object Code
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).
Preprocessor.
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
Announcements Final will be NEXT WEEK on August 17
Homework Finish up K&R Chapters 3 & 4
The Preprocessor Chapter 14 Copyright © 2008 W. W. Norton & Company.
C Preprocessor Seema Chandak.
Macro A fragment of code which has been given a name.
What Is? function predefined, programmer-defined
Conditional Compilation
SPL – PS1 Introduction to C++.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

14. THE PREPROCESSOR

Preprocessor Directives • Preprocessing involves making changes to the text of the source program. • Preprocessing is (apparently) done before actual compilation begins. • The preprocessor doesn’t know (very much) C. • Major kinds of preprocessor directives: Macro definition Conditional compilation File inclusion

Preprocessor Directives • Rules for using preprocessor directives: Must begin with a #. May contain extra spaces and tabs. End at the first new-line character, unless continued using \. Can appear anywhere in a program. Comments may appear on the same line.

Simple Macros • Form of a simple macro: #define identifier replacement-list The replacement list can be any sequence of C tokens, including identifiers, keywords, numbers, character constants, string literals, operators, and punctuation. • Uses of simple macros: Defining “manifest constants” Making minor changes to the syntax of C Renaming types As conditions to be tested later by the preprocessor

Simple Macros • Examples of simple macros: #define N 100 #define PI 3.14159 #define CR '\r' #define WARNING "Warning: nonstandard feature" #define BEGIN { #define END } #define BOOL int #define DEBUG • Warning: Don’t put any extraneous symbols in a macro definition; these will become part of the replacement list: #define N = 100 int a[N]; /* becomes int a[= 100]; */ #define N 100; int a[N]; /* becomes int a[100;]; */

Parameterized Macros • Form of a parameterized macro: #define identifier(x1, x2, …, xn) replacement-list There must be no space between the identifier and the left parenthesis. • Parameterized macros often serve as simple functions. • Examples of parameterized macros: #define MAX(x,y) ((x)>(y)?(x):(y)) #define EVEN(n) ((n) % 2 == 0) #define ODD(n) ((n) % 2 != 0) #define TOUPPER(c) ('a'<=(c)&&(c)<='z'?(c)-'a'+'A':(c)) #define getchar() getc(stdin)

Parameterized Macros • Advantages of using a parameterized macro instead of a function: The compiled code will execute more rapidly. Macros are “generic.” • Disadvantages of using a parameterized macro instead of a function: The compiled code will often be larger. Arguments aren’t type-checked. It’s not possible to have a pointer to a macro. A macro may evaluate its arguments more than once, causing subtle errors. • In C99, functions can be declared inline. A call of an inline function can be expanded by the compiler, yielding the speed advantage of a macro without the potential hazards.

The # Operator • Parameterized macros can be used to create templates for commonly used segments of code: #define PRINT_INT(x) printf("%d\n", x) PRINT_INT(i/j); /* becomes printf("%d\n", i/j); */

The # Operator • The # operator converts a macro argument into a string literal: #define PRINT_INT(x) printf(#x " = %d\n", x) PRINT_INT(i/j); The preprocessor will replace the call of PRINT_INT by the following code: printf("i/j" " = %d\n", i/j); which is equivalent to printf("i/j = %d\n", i/j); The output produced by this call of printf will have the following form: i/j = 5

General Properties of Macros • One macro may be defined in terms of another: #define PI 3.14159 #define TWO_PI (2*PI) When the preprocessor encounters the symbol TWO_PI later in the program, it replaces it by (2*PI). The preprocessor then rescans the replacement list to see if it contains invocations of other macros (PI in this case). The preprocessor will rescan the replacement list as many times as necessary to eliminate all macro names.

General Properties of Macros • The preprocessor replaces only entire symbols, not portions of symbols. It ignores macro names embedded in identifiers, character constants, and string literals. #define SIZE 256 char error_msg[] = "Error: SIZE exceeded"; /* not replaced */ ... if (BUFFER_SIZE > SIZE) /* only SIZE is replaced */ printf("%s\n", error_msg); • A macro definition normally remains in effect from the point at which it appears to the end of the file. However, a macro may be undefined by the following directive: #undef identifier

General Properties of Macros • Attempting to redefine a macro without undefining it first is illegal, unless the new definition is identical to the old one. • Attempting to undefine an undefined macro is legal.

Parentheses in Macro Definitions • When a macro’s replacement list is an expression, it should be enclosed in parentheses. Otherwise, the following may happen: #define TWO_PI 2*3.14159 ... conversion_factor = 360/TWO_PI; /* conversion_factor = 360/2*3.14159; */ • In a parameterized macro, each parameter should be enclosed in parentheses. If not, this may happen: #define SCALE(x) (x*10) j = SCALE(i+1); /* j = (i+1*10); */

Conditional Compilation • The #if directive tests an expression to determine whether or not a particular section of text should be included in a program. The #endif directive marks the end of the section: #if constant-expression … #endif • The operator defined can be used in an #if directive: #if defined(identifier)

Conditional Compilation • The #ifdef directive combines #if with defined: #ifdef identifier … #endif • The #ifndef directive is the opposite of #ifdef: #ifndef identifier

Conditional Compilation • #if, #ifdef, and #ifndef all allow #elif and #else clauses: if-header … #elif constant-expression #else #endif

Uses of Conditional Compilation • Writing code to run on different machines or under different operating systems: #if defined(WIN32) … #elif defined(MAC_OS) #elif defined(LINUX) #endif • Providing a default definition for a symbol: #ifndef BUFFER_SIZE #define BUFFER_SIZE 256

Uses of Conditional Compilation • Including debugging code: #ifdef DEBUG printf("Value of i: %d\n", i); printf("Value of j: %d\n", j); #endif • Temporarily disabling code that contains comments: #if 0 bkg_color = BLACK; /* set background color */ • Protecting header files from being included more than once.

File Inclusion • The #include directive causes the entire contents of a file to be included in a program. • Files included into a program are called header files (or include files). • By convention, header files have the extension .h. • One form of #include is used for files that belong to the C library: #include <filename> Most compilers will search the directory (or directories) where system header files are kept.

File Inclusion • The other form of #include is used for files created by the programmer: #include "filename" Most compilers will search the current directory, then search the directory (or directories) where system header files are kept. • File names may include a drive specifier and/or a path: #include <sys\stat.h> #include "utils.h" #include "..\include\utils.h" #include "d:utils.h" #include "\cprogs\utils.h" #include "d:\cprogs\utils.h"