More about the C Preprocessor CS-2303, C-Term 20101 More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The.

Slides:



Advertisements
Similar presentations
CSE 303 Lecture 16 Multi-file (larger) programs
Advertisements

Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Lecture 2 Introduction to C Programming
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Event-drive SimulationCS-2303, C-Term Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The.
Introduction: C Pointers Day 2 These Slides NOT From Text.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (Slides include materials from The.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Scope Rules and Storage Types CS-2303, C-Term Scope Rules and Storage Types CS-2303, System Programming Concepts (Slides include materials from The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Symbolic Constants in CCS-2303, C-Term Digression – Symbolic Constants in C CS-2303, System Programming Concepts (Slides include materials from The.
Introduction to C++CS-2303, C-Term Introduction to C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
CS-2303 System Programming Concepts
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.
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)
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
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.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
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.
Week 2-3 Control flow (review) Conditional statements If, else, else if, switch-case, break Loop constructs for, while, do-while, break, continue, label--go;
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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.
Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used to group declarations and.
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.
Makefiles Manolis Koubarakis Data Structures and Programming Techniques 1.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Introduction to C Topics Compilation Using the gcc Compiler
13 C Preprocessor.
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
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
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.
Miscellaneous C++ Topics
Symbolic Constants in C
Programming in C Miscellaneous Topics.
Your first C and C++ programs
Homework Finish up K&R Chapters 3 & 4
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

More about the C Preprocessor CS-2303, C-Term More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

More about the C Preprocessor CS-2303, C-Term The C Preprocessor A separate compilation step before the compiler tries to “understand” your program Simple text substitution –“On the fly” See K&R, §4.11 & §A12 D&D, Chapter 13

More about the C Preprocessor CS-2303, C-Term Familiar Preprocessor Directives #include "filename" #include –Inserts the entire contents of filename in place of the #include line. –Remember, all.c and.h files must end with newline character! #define NAME replacement-text –Substitutes replacement-text wherever NAME appears in source file. Difference: which directories to search for filename

More about the C Preprocessor CS-2303, C-Term Macro Substitution with Arguments #define NAME(A, B) repl_text with A & B Defines a macro in which –text of argument A is inserted into repl_text wherever parameter A occurs –text of argument B is inserted into repl_text wherever parameter B occurs Example:– #define max(A, B) ((A) > (B) ? (A) : (B)) –Replace max(x+1, y-2) with ((x+1) > (y-2) ? (x+1) : (y-2))

More about the C Preprocessor CS-2303, C-Term Hazards with Macros Side effects:– –max(x++, y--) expands to ((x++) > (y--) ? (x++) : (y--)) –I.e., x is incremented twice if greater, y is decremented twice if lesser Unintended consequences:– #define SQUARE(x) x * x –Then SQUARE(n + 1) expands to n + 1 * n + 1

More about the C Preprocessor CS-2303, C-Term Hazards with Macros (continued) Always surround parameters with () in replacement text To be sure that evaluation order of expression is what you really mean Never use expression with side-effects as argument to a pre-processor macro I.e., x++, a += b, etc.

More about the C Preprocessor CS-2303, C-Term Conditional Inclusion #if !defined(HDR) #define HDR /*contents of hdr.h are here */ #ENDIF Result:– –The file is only included once –Even if it is mentioned multiple times

More about the C Preprocessor CS-2303, C-Term Conditional Inclusion Example Suppose that your program says #include... It turns out that also includes Must avoid multiple copies of in your program Zillions of compile errors!

More about the C Preprocessor CS-2303, C-Term Conditional Inclusion Example (continued) Therefore, looks like #ifndef _STDIO_H #define _STDIO_H /* body of stdio.h */ #endif All system include files have the same form On any non-trivial project, your include files should take the same form. #ifndef NAME is same as #if !defined(NAME)

More about the C Preprocessor CS-2303, C-Term Another Conditional Include Example #if SYSTEM == SYSV #define HDR "sysv.h" #elif SYSTEM == BSD #define HDR "bsd.h" #elif SYSTEM == MSDOS #define HDR "msdos.h" #else #define HDR "default.h" #endif #include HDR K & R page 91 Selectively include the correct header file for the particular platform Value of system can be defined on gcc command line gcc –D SYSTEM=BSD … Widely used inside of OS and device drivers

More about the C Preprocessor CS-2303, C-Term Yet Another Conditional Include Example #if PROCESSOR == x86 /*C-code for Intel x86 */ #elif PROCESSOR == X86_64 /*C-code for Intel/AMD x86_64 */ #elif PROCESSOR == PPC /*C-code for IBM PowerPC */ #else #error PROCESSOR not supported #endif

More about the C Preprocessor CS-2303, C-Term Setting Variables from Compile Command To set a variable, include the following in your makefile :– gcc –D PROCESSOR=X86...

More about the C Preprocessor CS-2303, C-Term C Preprocessor Directives Widely used in large C projects Intellectually “messy” An essential part of supporting projects and applications on multiple platforms

More about the C Preprocessor CS-2303, C-Term Questions?