240-491 Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor 240-491 Special Topics in Comp. Eng.

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)
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.
 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.
CS 202 Computer Science II Lab Fall 2009 October 1.
 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
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.
The Pre-processor A unique feature of c language is the pre-processor. A program can use the tools provided by pre-processor to make his program easy to.
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.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Adv. UNIX: large/131 Advanced UNIX v Objectives of these slides: –learn how to write/manage large programs consisting of multiple files, which.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
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.
Engineering Computing I Chapter 4 Functions and Program Structure.
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.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.
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.
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.
 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
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.
Problem Session 4 Header Files and Unix. Where are they? You may need to examine the header file to determine what the data structures and functions provided.
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.
Programming in C Project Organization Compiler Directives.
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.
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Pre-processor Directives
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).
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
Homework Finish up K&R Chapters 3 & 4
C Preprocessor Seema Chandak.
C Programming Language
Macro A fragment of code which has been given a name.
Chapter 11: The Preprocessor
CSE 341 Lecture 22 Macros; extending Scheme's syntax
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng. 1 Semester 2, The Preprocessor

Adv. UNIX:pre/112 Overview 1. Preprocessor Features 2. Including Files 3. Symbolic Constants 4. Macros 5. Conditional Compilation

Adv. UNIX:pre/ Preprocessor Features v The preprocessor is part of the compiler –it modifies the C program before the program is compiled v The modifications involve text substitution –text in the program is replaced by other text continued

Adv. UNIX:pre/114 v Substitution is actually based on replacing C tokens by other text. v Examples of tokens in a line of C code –the code: x = inc(foo); –the tokens: x = inc ( foo ) ;

Adv. UNIX:pre/ Main Kinds of Substitutions v File Inclusion v Symbolic Constants v Macros v Conditional Compilation

Adv. UNIX:pre/ Seeing the Substitution  It is possible to execute only the preprocessor part of gcc : $ gcc -E examp.c  This call to gcc will output the modified version of examp.c –can be used for checking that the right substitutions are occurring

Adv. UNIX:pre/ Including Files (#include) v Some examples: #include /* from /usr/include */ #include “mydefs.h” /* from current dir */ #include /* from /usr/include/sys */ Each #include line in the program will be replaced by the contents of the named header file.

Adv. UNIX:pre/ What’s in a header file? v Headers are C code (so have a look). v Typical code: –symbolic constant definitions ( #define lines) –typedef declarations –extern declarations (see later) –function prototypes v You will write your own header files when coding large, multiple file programs –see later

Adv. UNIX:pre/ Symbolic Constants (#define) v Some examples: #define TRUE 1 #define FALSE 0 #define SIZE 10 #define MAX (SIZE*2 + 1)  Common mistake – adding a ‘ ; ’ # define SIZE 10; while (x < SIZE)... becomes while (x < 10;)... The preprocessor replaces SIZE by 10; a constant defined using another constant

Adv. UNIX:pre/ Macros v Some examples: #define sum(x, y) ((x) + (y)) #define cube(x) ((x) * (x) * (x)) #define print(str) { printf(“%s\n”, str); } v Use:Translation: c = sum(a, b);c = ((a) + (b)); y = cube(a);y = ((a) * (a) * (a)); print(argv[1]);{ printf(“%s\n”, argv[1]); } the macro body

Adv. UNIX:pre/ Rules you Must Follow v 1. Don’t place ‘;’s at the end of a macro body. v 2. Bracket every parameter in the macro body. v 3. Bracket the entire macro body. v 4. Avoid side-effects in macro calls. v 5. Add braces around multiple statements in a macro body; have a ‘;’ after every statement. When in doubt: add some brackets

Adv. UNIX:pre/ Macros are not Functions v A macro ‘call’ is textually replaced by the macro body inside the preprocessor. v There is no overhead of a function call at run-time. v Macro replacement causes the size of the program to increase. v Lots of tricky errors are possible.

Adv. UNIX:pre/ Multi-line Macros v Big macros use ‘\’ to run over multiple lines: #define swap_int(s1, s2) { int temp; \ temp = s1; \ s1 = s2; \ s2 = temp; }

Adv. UNIX:pre/ Some Common Errors 1. Including space after the macro name. 2. Missing parameter brackets. 3. Missing macro body brackets. 4. Including side effects in macros.

Adv. UNIX:pre/ Space after macro name v Example: #define abs (x) ((x)> 0) ? (x) : (-(x))) /* WRONG */ v Use: x = abs(2) ; v Expansion: x = (x) ((x)> 0) ? (x) : (-(x)))(2); replaced by space after abs

Adv. UNIX:pre/ Missing parameter brackets v Example: #define cube(x) x * x * x /* WRONG */ v Use: y = cube(z + 1); v Expansion: y = z + 1 * z + 1 * z + 1; y = z + 1 * z + 1 * z + 1; same as: y = z + (1 * z) + (1 * z) + 1; no brackets around the x's continued

Adv. UNIX:pre/1117 v Correct Version: #define cube(x) ((x) * (x) * (x)) v Use: y = cube(z + 1); v Expansion: y = ((z + 1) * (z + 1) * (z + 1));

Adv. UNIX:pre/ Missing macro body brackets v Example: #define inc(x)(x) + 1/* WRONG */ v Use: p = 3 * inc(q); v Expansion: p = 3 * (q) + 1; p = 3 * (q) + 1; same as: p = (3 *(q)) + 1; no brackets around the entire macro body continued

Adv. UNIX:pre/1119 v Correct version: #define inc(x)((x) + 1) v Use: p = 3 * inc(q); v Expansion: p = 3 * ((q) + 1);

Adv. UNIX:pre/ Side effects v Example: #define cube(x)((x) * (x) * (x)) v Use: int a = 2; c = cube(a++); v Expansion: int a = 2; c = ((a++) * (a++) * (a++)) a's value is side-effected three times

Adv. UNIX:pre/ More Examples v /* macros instead of functions */ #define max(x, y)((x) > (y) ? (x) : (y)) #define sign(x)((x) 0 ? 1 : 0)) v /* macros for control flow */ #define foreverfor(;;) #define repeatdo { #define until(x)} while (!(x)) v /* nicer names */ #define TRUE1 #define FALSE0 continued to be used as a pair

Adv. UNIX:pre/1122 v /* graphics macro */ #define line(x1,y1,x2,y2) { moveto(x1, y1) ; \ lineto(x2, y2); } v /* macro instead of function, with type info */ #define swap(x, y, TYPE){ TYPE temp; \ temp = x; \ x = y; \ y = temp; } v /* hide pointers without using functions */ #define LEFT(ptr)(ptr->left) #define RIGHT(ptr)(ptr->right) #define DATA(ptr)(ptr->data)

Adv. UNIX:pre/ Undefining v Cancel a definition: #undef name –applies from this line in the text downwards v Can redefine later: #define name 10

Adv. UNIX:pre/ Conditional Compilation v Specify which blocks of code to compile –some parts of the program can be ignored by the compiler v Used for debugging and portability.

Adv. UNIX:pre/ If Tests v General format: #if expression.../* code to be included if expression is true */ #endif v Expressions are C-like, but can only refer to symbolic constants and/or macros.

Adv. UNIX:pre/1126 Examples #define MAX 9 : #if MAX < /* code to include */ #endif #define X 5 #define Y7 #define DEBUG 2 : #if X < Y && DEBUG == 1... /* code to include */ #endif

Adv. UNIX:pre/1127 Leaving Out Code v Example: # if 0.../* lines of code to be left out */ #endif v Useful for commenting out lines that contain lots of comments –cannot nest comments in C

Adv. UNIX:pre/ Multi-way Branches v Format: #if expr1... #elif expr2... #elif expr3... : #else... #endif

Adv. UNIX:pre/ Definition Test  #ifdef name Test if name defined #ifndef name Test if name not defined v Used for debugging: #define DEBUG : #ifdef DEBUG printf(“Some debug message\n”): #endif continued

Adv. UNIX:pre/1130 v Or: #define DEBUG 1 : #if DEBUG == 1 printf(“Some debug message\n”); #endif  In this version, we must supply a value for DEBUG. continued

Adv. UNIX:pre/1131 v Prevent multiple includes: #ifndef INCLUDED_TYPES #define INCLUDE_TYPES #include “types.h” #endif v Implement version differences: #ifdef UNIX char *version = “UNIX version”; #else char *version = “DOS version”; #endif only include types.h if INCLUDE_TYPES isn't already defined

Adv. UNIX:pre/ Creating Symbolic Constants  Can use #define or v Create (and initialise) on the command line: $ gcc -DDEBUG -o examp examp.c /* define DEBUG */ $ gcc -DMAX=2 -o foo foo.c /* define MAX with value 2 */

Adv. UNIX:pre/ Error Reporting (#error) v Example: #ifndef UNIX #error UNIX version not chosen. #endif v Or: #if !defined(UNIX) #error UNIX version not chosen #endif