COM S 326X Deep C Programming for the 21st Century Prof. Rozier

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)
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
 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.
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.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
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.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
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.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
 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.
1 Introduction Purpose  This course gives an overview of assembler settings of the High-performance Embedded Workshop (HEW), used for developing software.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Chapter 14: The Preprocessor Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 14 The Preprocessor.
 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.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
13 C Preprocessor.
ECE 382 Lesson 20 Lesson Outline Structs Functions Headers Example
Announcements Midterm2 Grades to be announced THIS WEEK
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
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.
Programmazione I a.a. 2017/2018.
Pre-processor Directives
Instructor: Ioannis A. Vetsikas
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.
Announcements Final will be NEXT WEEK on August 17
Writing Large Programs
C Preprocessor Seema Chandak.
C Programming Language
Chapter 11: The Preprocessor
The Role of Command Line Compiler (csc.exe)
Compiler vs linker The compiler translates one .c file into a .o file
CMSC 202 Exceptions.
GCC: the GNU Compiler Collection
Conditional Compilation
SPL – PS1 Introduction to C++.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

COM S 326X Deep C Programming for the 21st Century Prof. Rozier Unit 5 – The Preprocessor

History of C A number of changes occurred to C from 1972-1973, one of the most important was the introduction of the Preprocessor. Alan Snyder urged for its inclusion: http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-149.pdf Added file-inclusion available in BCPL and PL/I

C Preprocessor Original version was very simple #include #define Extended by Mike Lesk and John Reiser to include macros with arguments and conditional compilation. Originally an “optional adjunct” to the C language.

Invoking the C Preprocessor Invoked by default. gcc –E file.c –o file.i Invokes just the preprocessor

C Preprocessor Contains directives Special instructions to the preprocessor or compiler on how to process some or all of the source code.

#include Many of C’s features are not part of the language, but instead part of the standard library. The standard library is a repository of code available alongside every standard-conformant C compiler. Usually linked in by the compiler unless otherwise specified.

#include What #include actually does is cause the preprocessor to include the contents of the file where the #include statement was. Try looking at the output of: gcc –E file.c –o file.i when file.c contains “#include <stdio.h>”

#include <header.h> Two main ways to #include: #include <header.h> #include “header.h” When using <header.h> the compiler searches for the include along the development environment path for standard includes Type: echo | gcc –E –Wp,-v – To find the paths searched. No space!

#include When using the form “header.h” the preprocessor searches user-defined locations, including the current directory (usually) for header files before searching the standard path.

Include Guards The preprocessor is not intelligent. It is possible to have the problem of double includes. foo.h includes bar.h main.c includes foo.h and bar.h bar.h is now included twice!

Include guards Some compilers support the preprocessor command: #pragma once This isn’t universally supported, so it is non-portable. More commonly we see: #ifndef __HEADER_H__ #define __HEADER_H__ … #endif

Include guards Checks if __HEADER_H__ has been defined #ifndef __HEADER_H__ #define __HEADER_H__ void foo(int, int); #endif If not, the compiler includes all this code, including a definition for __HEADER_H__ Otherwise this block is removed by the preprocessor.

Include Guard Example Download the include guard example from the course website with the following command: wget http://dataengineering.org/erozier/COMS326XF17/include_guard.tgz Extract the source files with following command: tar –xvf includeguard.tgz Work in groups of 2-3, read through the files and predict their behavior. Run the preprocessor with: gcc –E include_guard.c –o include_guard.i Did the outcome match your expectations? Note: The #warning directive is used to issue compiler messages so you can follow the control flow

#define Can be used to define values, macros, or just symbols: #define iastate Creates the symbol iastate, it exists, but has no interesting definition.

#define Can be used to define values, macros, or just symbols: #define IASTATE Creates the symbol iastate, it exists, but has no interesting definition. By convention, we use uppercase identifiers for the preprocessor.

#define Can be used to define values, macros, or just symbols: #define IASTATE 10 Creates the symbol iastate, and substitutes it for value 10 where it appears.

#define Can be used to define values, macros, or just symbols: #define MAX(x,y) ((x)>(y)?(x):(y)) Creates the symbol MAX, takes two arguments, and builds a pseudofunction. This can be dangerous. The macro just substitutes, but does not evaluate. Can lead to strange results.

#define dangers Look at the following two macros: #define MAXA(x,y) x>y?x:y #define MAXB(x,y) ((x)>(y)?(x):(y)) In groups of 2-3 implement both macros. See how they work with the following: i = MAXA(2,3)+5 j = MAXA(3,2)+5 m = MAXB(2,3)+5 n = MAXB(3,2)+5 What happens? Why? Try running the compiler with –E to examine the output.

#define tricks The # symbol has two special tricks with #define: #define as_string(a) # a #define concatinate(x, y) x ## y Try these #define macros and discuss their results.

#define for debug messages What does the following do? You may try this on the server to explore its outcome. #define num2str(x) str(x) #define str(x) #x #ifdef DEBUG #define debug(msg) fputs(__FILE__ “:” num2str(__LINE__) “ – “ msg, stderr) #else #define debug(msg) #endif

Useful Preprocessor Macros and Variables __FILE__  The name of the current file, as a string literal __LINE__  Current line of the source file, as a numeric literal __DATE__  Current system date, as a string __TIME__  Current system time, as a string __TIMESTAMP__  Date and time (non-standard) __func__  Current function name of the source file, as a string (part of C99) __PRETTY_FUNCTION__  "decorated" Current function name of the source file, as a string (in GCC; non-standard)

gcc –D option You can use the compiler to define macros, or symbols: Useful for debugging! Can be defined using Makefiles!

#define for debug messages Now consider this case with Makefile targets for debug and normal. How would you change to a debug context? #define num2str(x) str(x) #define str(x) #x #ifdef DEBUG #define debug(msg) fputs(__FILE__ “:” num2str(__LINE__) “ – “ msg, stderr) #else #define debug(msg) #endif

Compiler directives #error message – halt compilation and produce “message” on the output. #warning message – emit diagnostic message during compilation when encountered.

#undef #undef TOKEN – undefines TOKEN.

Conditionals on defines #ifdef TOKEN #ifndef TOKEN Excecutes a block until #endif, #else, #elif encountered if TOKEN is defined, or if it is not defined, respectively.

#if, #else, #elif, #endif General conditionals. #if <expression> - evaluates a block if the expression is nonzero. #else – evaluates a block if the expression for the paired #if was zero. #elif <expression> – evaluates if expression is nonzero, and all previous expressions in the #if block were zero.

Advanced preprocessor tricks X-Macro pattern – NOT RECOMMENDED Shows some of the power of the preprocessor. Examine the code contained on the website in: student-datatype.tgz What does this set of macros do? Execute gcc with the –E flag to examine the output. If you compile the code, use the –c flag to generate an object file, no main means it won’t build a full executable.

Advanced preprocessor tricks X-Macro pattern – NOT RECOMMENDED Shows some of the power of the preprocessor. Examine the code contained on the website in: student-datatype.tgz Why might this be useful? Why might it be dangerous? Why might it be a bad design pattern for C programming?