CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
The C Preprocessor Yongjoon Lee April 22, What is it? Things with # –#include Processes the C source files BEFORE handing it to compiler. –`Pre`-process.
 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.
CSc 352 Shell Scripts Saumya Debray Dept. of Computer Science
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
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.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
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.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter 6: User-Defined Functions
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.
Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
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.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
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.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 8 – C: Miscellanea Control, Declarations, Preprocessor, printf/scanf.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
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.
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.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
C++ Functions A bit of review (things we’ve covered so far)
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.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 13 – C: The Rest of the Preprocessor.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
What Is? function predefined, programmer-defined
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Pre-processor Directives
User-Defined Functions
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.
Comments, Prototypes, Headers & Multiple Source Files
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Code Organization CSCE 121 J. Michael Moore.
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Conditional Compilation
Presentation transcript:

CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson

The C preprocessor and its role 2 cpp (C preprocessor) cc1 (C compiler) source program compiled code C compiler (e.g., gcc) expanded code expand some kinds of characters discard whitespace and comments – each comment is replaced with a single space process directives: – file inclusion (#include) – macro expansion (#define) – conditional compilation (#if, #ifdef, …) text: Ch. 14

#include Specifies that the preprocessor should read in the contents of the specified file – usually used to read in type definitions, prototypes, etc. – proceeds recursively #includes in the included file are read in as well Two forms: – #include searches for filename from a predefined list of directories the list can be extended via “gcc – I dir” – #include “ filename ” looks for filename specified as a relative or absolute path 3

#include : Example 4 a predefined include file that: comes with the system gives type declarations, prototypes for library routines (printf) where does it come from? – man 3 printf :

#include: cont’d We can also define our own header files: – a header file has file-extension ‘.h’ – these header files typically contain “public” information type declarations macros and other definitions function prototypes – often, the public information associated with a code file foo.c will be placed in a header file foo.h – these header files are included by files that need that public information #include “myheaderfile.h” 5

Macros A macro is a symbol that is recognized by the preprocessor and replaced by the macro body – Structure of simple macros: #define identifier replacement_list – Examples: #define BUFFERSZ 1024 #define WORDLEN 64 6

Using simple macros We just use the macro name in place of the value, e.g.: #define BUFLEN 1024 #define Pi … char buffer[BUFLEN]; … area = Pi * r * r; 7 NOT: #define BUFLEN = 1024 #define Pi ; 

Example 1 8

Example 2 9 we can “macroize” symbols selectively

Parameterized macros Macros can have parameters – these resemble functions in some ways: macro definition ~ formal parameters macro use ~ actual arguments – Form: #define macroName(arg 1, …, arg n ) replacement_list – Example: #define deref(ptr) *ptr #define MAX(x,y) x > y ? x : y 10 no space here! (else preprocessor will assume we’re defining a simple macro

Example 11

Macros vs. functions Macros may be (slightly) faster – don’t incur the overhead of function call/return – however, the resulting code size is usually larger this can lead to loss of speed Macros are “generic” – parameters don’t have any associated type – arguments are not type-checked Macros may evaluate their arguments more than once – a function argument is only evaluated once per call 12

Macros vs. Functions: Argument Evaluation Macros and functions may behave differently if an argument is referenced multiple times: – a function argument is evaluated once, before the call – a macro argument is evaluated each time it is encountered in the macro body. Example: 13 int dbl(x) { return x + x;} … u = 10; v = dbl(u++); printf(“u = %d, v = %d”, u, v); prints : u = 11, v = 20 #define Dbl(x) x + x … u = 10; v = Dbl(u++); printf(“u = %d, v = %d”, u, v); prints : u = 12, v = 21 Dbl(u++) expands to: u++ + u++

Properties of macros Macros may be nested – in definitions, e.g.: #define Pi #define Twice_Pi 2*Pi – in uses, e.g.: #define double(x) x+x #define Pi … if ( x > double(Pi) ) … Nested macros are expanded recursively 14

Pitfalls of nested macros 15 Oops!

What happened? 16 textual replacement!

Avoiding the problem 17

What happened 18

Header Files Have a file extension “.h” Contain shared definitions – typedefs – macros – function prototypes referenced via “#include” directives 19

Header files: example 20

typedefs Allow us to define aliases for types Syntax: typedef old_type_name new_type_name; new_type_name becomes an alias for old_type_name Example: – typedef int BasePay; – typedef struct node { int value; struct node *next; } node; 21

Example 22 defines “wcnode” as an alias for “struct wc” we can use “wcnode” in place of“struct wc” but not here, since “wcnode” has not yet been defined

What if a file is #included multiple times? 23 foo.h bar1.h bar2.h bar.c

Example of multiple inclusions 24

Problems with multiple inclusions 25

Solution to multiple inclusion problem Use conditional compilation to ensure that a header file is “really included” at most once – header file’s responsibility to protect itself from multiple- inclusion problems – uses a conditional-compilation directive #ifndef – in effect sets a flag when a file is included so we don’t include it again – relies on convention we need to understand it so we don’t break it 26

Conditional Compilation: #ifdef #ifdef identifier line 1 … line n #endif macros can be defined by the compiler: – gcc –D macroName – gcc –D macroName=definition macros can be defined without giving them a specific value, e.g.: – #define macroName 27 line1 … line n will be included if identifier has been defined as a macro; otherwise nothing will happen.

Conditional Compilation: #ifndef #ifndef identifier line1 … line n #endif 28 line1 … line n will be included if identifier is NOT defined as a macro; otherwise nothing will happen.

Solution to multiple inclusion problem The header file is written as follows: #ifndef file_specific_flag #define file_specific_flag …contents of file… #endif file_specific_flag usually constructed from the name of the header file: E.g.: file = foo.h  flag = _FOO_H_ – try to avoid macro names starting with ‘_’ 29 indicates whether or not this file has been included already

Another use of #ifdefs They can be useful for controlling debugging output – Example 1: guard debugging code with #ifdefs: #ifdef DEBUG …debug message… #endif – Example 2: use the debug macro to control what debugging code appears in the program: #ifdef DEBUG #define DMSG(msg) printf(msg) // debugging output #else #define DMSG(msg) {} // empty statement #endif 30 straightforward, but needs discipline to use consistently

Example 1(a) 31

Example 1(b) 32 to address the “too many arguments to macro” problem “too many arguments” issue resolved, but the macro expansion isn’t working quite the way we want

Example 1(c) 33

Generalizing #ifdef #if constant-expression line 1 … line n #endif  line 1 … line n included if constant-expression evaluates to a non-zero value 34 Common uses: #if 1 or #if 0