CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 13 – C: The Rest of the Preprocessor.

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)
CSE 303 Lecture 16 Multi-file (larger) programs
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Week 5 - Wednesday.  What did we talk about last time?  Arrays.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
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.
ספטמבר 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.
 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.
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Object-Oriented Programming in C++
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 Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.
Engineering Computing I Chapter 4 Functions and Program Structure.
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
 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.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
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.
C++ Functions A bit of review (things we’ve covered so far)
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 18 – Linking and Libraries.
Programs – Preprocessing, Compilation and Linking
Lecture 3 Translation.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
What Is? function predefined, programmer-defined
CSE 374 Programming Concepts & Tools
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
CSE 374 Programming Concepts & Tools
Functions Separate Compilation
14. THE PREPROCESSOR.
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Pre-processor Directives
Preprocessor C program → Modified C program → Object Code
Structures putting data together.
C Preprocessor(CPP).
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
What Is? function predefined, programmer-defined
Conditional Compilation
Week 2 - Friday CS222.
CSE 303 Concepts and Tools for Software Development
Week 5 - Wednesday CS222.
Presentation transcript:

CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 13 – C: The Rest of the Preprocessor

Administrivia Midterms are mostly graded – sample solution + scores posted sometime tomorrow, exams returned at end of class Friday HW5 due next Thursday night –Demo/discussion today –Must read assignment carefully by Friday so we can take questions in class & so you can make serious progress over the weekend HW6 – multiple parts with a partner –Pick a partner by end of next week. Partner info must be submitted online by 11pm next Friday (details next week, cannot send that info late) 2

The story so far… We’ve looked at the basics of the preprocessor –#include to access declarations in header files –#define for symbolic constants Now: –More details; where it fits –Multiple source and header files –A bit about macros (somewhat useful, somewhat a warning) 3

The compilation picture gcc does all this for you (reminder) -E to only preprocess; result on stdout (rare) -c to stop with.o (common for individual files in larger program) 4

More about multiple files Typical usage: Preprocessor #include to read file containing declarations describing code Linker handles your.o files and other code –By default, the “standard C library” –Other.o and.a files –Whole lecture on linking and libraries later… 5

The preprocessor Rewrites your.c file before the compiler gets at the code. –Lines starting with # tell it what to do Can do crazy things (please don’t); uncrazy things are: 1.Including contents of header files 2.Defining constants and parameterized macros Token-based, but basically textual replacement Easy to misdefine and misuse 3.Conditional compilation Include/exclude part of a file Example uses: code for debugging, code for particular computers (handling portability issues), “the trick” for including header files only once 6

File inclusion (review) #include Search for file hdr.h in “standard include directories” and include its contents in this place –Typically lots of nested includes, result not fit for human consumption –Idea is simple: declaration of standard library routines are in headers; allows correct use after declaration #include “hdr.h” –Same, but first look in current directory –How to break your program into smaller files that can call routines in other files gcc -I option: look first in specified directories for headers (keep paths out of your code files) (not needed for 374) 7

Header file conventions Conventions: always follow these when writing a header file 1.Give included files names ending in.h; only include these header files. Never #include a.c source file 2.Do not put functions definitions in a header file; only struct definitions, prototypes (declarations), and other includes 3.Do all your #includes at the beginning of a file 4.For header file foo.h start it with: #ifndef FOO_H #define FOO_H and end it with: #endif (We will learn why very soon) 8

Simple macros (review) Symbolic constants and other text #define NOT_PI 22/7 #define VERSION 3.14 #define FEET_PER_MILE 5280 #define MAX_LINE_SIZE 5000 Replaces all matching tokens in rest of file –Knows where “words” start and end (unlike sed) –Has no notion of scope (unlike C compiler) –(Rare: can shadow with another #define or use #undef to remove) 9

Macros with parameters #define TWICE_AWFUL(x) x*2 #define TWICE_BAD(x) ((x)+(x)) #define TWICE_OK(x) ((x)*2) double twice(double x) { return x+x; } // best (editorial opinion) Replace all matching “calls” with “body” but with text of arguments where the parameters are (just string substitution) Gotchas (understand why!): y=3; z=4; w=TWICE_AWFUL(y+z); y=7; z=TWICE_BAD(++y); z=TWICE_BAD(y++); Common misperception: Macros avoid performance overhead of a function call (maybe true in 1975, not now) Macros can be more flexible though (TWICE_OK works on ints and doubles without conversions (which could round)) 10

Justifiable uses Parameterized macros are generally to be avoided (use functions), but there are things functions cannot do: #define NEW_T(t,howmany) ((t*)malloc((howmany)*sizeof(t)) #define PRINT(x) printf("%s:%d %s\n", __FILE__, __LINE__,x) 11

Conditional compilation #ifdef FOO (matching #endif later in file) #ifndef FOO (matching #endif later in file) #if FOO > 2 (matching #endif later in file) (You can also have a #else inbetween somewhere.) Simple use: #ifdef DEBUG // do following only when debugging printf(...); #endif Fancier: #ifdef DEBUG // use DBG_PRINT for debug-printing #define DBG_PRINT(x) printf("%s",x) #else #define DBG_PRINT(x) // replace with nothing #endif Note: gcc -D FOO makes FOO “defined” 12

Back to header files Now we know what this means: #ifndef SOME_HEADER_H #define SOME_HEADER_H... rest of some_header.h... #endif Assuming nobody else defines SOME_HEADER_H (convention), the first #include "some_header.h" will do the define and include the rest of the file, but the second and later will skip everything –More efficient than copying the prototypes over and over again –In presence of circular includes, necessary to avoid “creating” an infinitely large result of preprocessing So we always do this 13

C preprocessor summary A few easy to abuse features and a bunch of conventions (for overcoming C’s limitations). –#include (the way you say what other definitions you need; cycles are fine with “the trick”) –#define (avoids magic constants; parameterized macros have a few justifiable uses; token-based text replacement) –#if... (for showing the compiler less code) 14