1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp

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)
Files in C Rohit Khokher.
CSE 303 Lecture 16 Multi-file (larger) programs
David Notkin Autumn 2009 CSE303 Lecture 20 Multi-file (larger) programs Friday: social implications.
David Notkin Autumn 2009 CSE303 Lecture 16 #preprocessor Debugging is twice as hard as writing the code in the first place. Therefore, if you write the.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
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.
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.
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.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
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.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
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.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
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.
An Introduction to C++ A First Look. void Functions #include #include void main( ) { cout
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.
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.
 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.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
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.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
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.
CISC105 – General Computer Science
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Preprocessor Directives
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).
Preprocessor.
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
CSE 341 Lecture 22 Macros; extending Scheme's syntax
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp

2 C preprocessor preprocessor : Part of the C compilation process; recognizes special # statements, modifies your source code before it is compiled functiondescription #include insert a library file's contents into this file #include "filename" insert a user file's contents into this file #define name [value] create a preprocessor symbol ("variable") #if test if statement #else else statement #elif test else if statement #endif terminates an if or if/else statement #ifdef name if statement; true if name is defined #ifndef name if statement; true if name is not defined #undef name deletes the given symbol name

3 Constants The preprocessor can be used to create constants: #define NUM_STUDENTS 100 #define DAYS_PER_WEEK 7... double grades[NUM_STUDENTS]; int six_weeks = DAYS_PER_WEEK * 6; // 42 printf("Course over in %d days", six_weeks);  When the preprocessor runs before compilation, 7 is literally inserted into the code wherever DAYS_PER_WEEK is seen the name DAYS_PER_WEEK does not exist in the eventual program int six_weeks = 7 * 6; // 42

4 Debugging code The preprocessor is often used to include optional debug code: #define DEBUG... #ifdef DEBUG // debug-only code printf("Size of stack = %d\n", stack_size); printf("Top of stack = %p\n", stack); #endif stack = stack->next; // normal code  How is this different from declaring a bool / int named DEBUG ?

5 Advanced definitions #define can be used to dialect the C language: #define AND && #define EQUALS == #define DEREF ->... Point p1 = (Point*) malloc(sizeof(Point)); p1 DEREF x = 10; p1 DEREF y = 10; if (p1 DEREF x EQUALS p1 DEREF y AND p1 DEREF y > 0) { p1 DEREF x++; }  Warning: Evil may result.

6 Preprocessor macros #define can accept arguments to create a macro.  sort of like a function, but injected inline before compilation #define SQUARED(x) x * x #define ODD(x) x % 2 != 0... int a = 3; int b = SQUARED(a); if (ODD(b)) { printf("%d is an odd number.\n", b); }  The above literally converts the code to the following and compiles: int b = a * a; if (b % 2 != 0) {...

7 Subtleties the preprocessor is dumb; it just replaces tokens with tokens #define foo 42 int food = foo; // int food = 42; ok int foo = foo + foo; // int 42 = ; bad preprocessor macros can do a few things functions cannot: #define NEW(t) (t*) calloc(1, sizeof(t))... Node* list = NEW(Node);

8 Caution with macros since macros are injected directly, strange things can happen if you pass them complex values #define ODD(x) x % 2 != 0... if (ODD(1 + 1)) { printf("It is odd.\n"); // prints! }  The above literally converts the code to the following and compiles: if (1 + 1 % 2 != 0) {  Fix: Always surround macro parameters in parentheses. #define ODD(x) (x) % 2 != 0

9 Running the preprocessor to run only the preprocessor, use the -E argument to gcc : $ gcc -E example.c int main(void) { if ((1 + 1) % 2 != 0) { printf("It is odd.\n"); } return 0; }  outputs the result of preprocessing example.c to standard-out; rarely used in practice, but can be useful for debugging / learning to define a preprocessor variable, use the -D variable argument: $ gcc -D DEBUG -o example example.c