CSE 374 Programming Concepts & Tools

Similar documents
CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Conditional Compilation

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33

Chapter 7: Preprocessing Directives

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

Software Engineering /48

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

Final C Details. CSE 333 Autumn 2018

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34

A Fast Review of C Essentials Part II

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CS3157: Advanced Programming. Outline

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

CSE 303: Concepts and Tools for Software Development

CS240: Programming in C

OBJECT ORIENTED PROGRAMMING USING C++

CSE 374 Programming Concepts & Tools

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Have examined process Creating program Have developed program Written in C Source code

Lecture 10: building large projects, beginning C++, C++ and structs

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

CSE 333 Lecture 5 - data structures & modules

COMsW Introduction to Computer Programming in C

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

CSE 333 Lecture 6 - data structures

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Topic 6: A Quick Intro To C

Appendix A. The Preprocessor

Errors During Compilation and Execution Background Information

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

The C Preprocessor (and more)!

COMP322 - Introduction to C++

Intermediate Programming, Spring 2017*

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

#include. Practical C Issues: #define. #define Macros. Example. #if

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

CSE 374 Programming Concepts & Tools

Better variadic functions in C

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

TDDE18 & 726G77. Functions

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Lecture 13: more class, C++ memory management

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

CSE 333 Midterm Exam July 24, Name UW ID#

PROGRAMMAZIONE I A.A. 2017/2018

CIS 190: C/C++ Programming. Classes in C++

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Final C Details. CSE 333 Winter Teaching Assistants: Alexey Beall Renshu Gu Harshita Neti David Porter Forrest Timour Soumya Vasisht

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Computational Methods of Scientific Programming Fall 2007

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

QUIZ. Source:

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

G52CPP C++ Programming Lecture 9

Programming for Engineers C Preprocessor

CSE341: Programming Languages Lecture 15 Macros. Zach Tatlock Winter 2018

What is a macro. CSE341: Programming Languages Lecture 15 Macros. Example uses. Using Racket Macros. Zach Tatlock Winter 2018

1 You seek performance 3

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

The Compilation Process

Programming. Projects with Multiple Files

Operator overloading

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

C and C++ 2. Functions Preprocessor. Alan Mycroft

Short Notes of CS201

CS 220: Introduction to Parallel Computing. Arrays. Lecture 4

CSE 333 Autumn 2014 Midterm Key

BLM2031 Structured Programming. Zeyneb KURT

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

EL6483: Brief Overview of C Programming Language

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

CS201 - Introduction to Programming Glossary By

Unit 4 Preprocessor Directives

Semantic Analysis. Lecture 9. February 7, 2018

Transcription:

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

Administrivia Midterm exam Wednesday, here Topics: everything up to hw4 (including gdb concepts, but not dynamic memory/malloc or structs) Old exams on web now for review/practice Review Q&A tomorrow, 4:30 MOR 230 HW5 out Wednesday after exam, due in a week Short demo today HW6 multiple parts with a partner Pick a partner by early next week. Partner info must be submitted online by 11 pm next Wednesday (details on how/where later this week) 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 combines 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 <hdr.h> 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. Put 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