CS 2130 Lecture 4 C Preprocessor. Typical "Includes" #include.

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Names and Bindings.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Michael Jackson Thriller. Biografy King of Pop, Michael Joe Jackson, and singer and dancer, was also known for his eccentricities. The singer joined the.
Structure of a C program
Chapter3: Language Translation issues
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
Guide To UNIX Using Linux Third Edition
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
C++ for Engineers and Scientists Third Edition
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Halloween Soundscape Inverurie Academy Faculty of Creative Arts: S3 Music.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
C Hints and Tips The preprocessor and other fun toys.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
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.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Minimal standard C program int main(void) { return 0 ; }
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
C is a high level language (HLL)
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Lecture 3: Getting Started & Input / Output (I/O)
Chapter 15 - C++ As A "Better C"
Chapter 7: User-Defined Functions II
Chapter 6 CS 3370 – C++ Functions.
CSE 220 – C Programming C Fundamentals.
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.
Lecture 11 bash scripting overview c programming overview moving data between c and bash memory and pointers.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
CS 536 / Fall 2017 Introduction to programming languages and compilers
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
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,
Creating your first C program
Procedure Activations
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
Presentation transcript:

CS 2130 Lecture 4 C Preprocessor

Typical "Includes" #include

Back to Hello World #include int main(void) { printf(“Hello, World!\n”); return EXIT_SUCCESS; } OK?

Doesn’t printf return something? printf(3) returns number of characters transferred Therefore >= 0: Success < 0: Failure Probably not worthwhile to check return value Could do this: (void)printf(… Which means ignore return value BUT THIS COULD BE A REAL BAD IDEA

Some simple streams By default we are supplied the following streams stdin: Keyboard stdout: Screen stderr: Screen

So we could do... if(printf(…) < 0) { fprintf(stderr,”printf failed\n”); /* perhaps exit here? */ } One slight problem!

So we could do... if(printf(…) < 0) { (void)fprintf(stderr,”printf failed\n”); /* perhaps exit here? */ } Does it make sense to use (void) in front of fprintf? Does it make sense to use (void) in front of fprintf?

Three Exceptions AllAll function return values must be checked except (void)fprintf(stderr,… (void)fflush(stderr); (void)exit(EXIT_FAILURE); WITHOUT FAIL!!!

How you should feel (for the rest of your life) if you forget to check a return value Darkness falls across the land The midnight hour is close at hand Creatures crawl in search of blood To terrorize your neighbor's hood And whosoever shall be found Without the soul for getting down Must stand and face the hounds of hell And rot inside a corpse's shell The foulest stench is in the air The funk of forty thousand years And grizzly ghouls from every tomb Are closing in to seal your doom And though you fight to stay alive Your body starts to shiver For no mere mortal can resist The evil of the 2130 killers

Getting out right now! exit(EXIT_FAILURE); Why not just: return EXIT_FAILURE;

Macro Processing Would like a simple way of performing if(printf(…) < 0) { (void)fprintf(stderr,”printf failed\n”); /* perhaps exit here? */ } Macro Processing could be the answer but first...

When Things Happen Key focus of CS 2130 is understanding when things happen During Translation (Compilation) During Execution

Translation (Compilation) 1. Lexical Analysis (Scanning) –Remove Comments –Find Keywords (Reserved Words) 2. Syntax Analysis –Check for structural correctness 3. Semantic Analysis –Concerned with meaning 4. Code Generation 5. Optimization –Time –Space –Global vs. Peephole

Translation (Compilation) 1. Lexical Analysis (Scanning) –Remove Comments –Find Keywords (Reserved Words) 2. Syntax Analysis –Check for structural correctness 3. Semantic Analysis –Concerned with meaning 4. Code Generation 5. Optimization –Time –Space –Global vs. Peephole Front End (Analysis) Back End (Synthesis) Primary Concern in CS 2130

Translation (Compilation) 1. Lexical Analysis (Scanning) –Remove Comments –Find Keywords (Reserved Words) 2. Syntax Analysis –Check for structural correctness 3. Semantic Analysis –Concerned with meaning 4. Code Generation 5. Optimization –Time –Space –Global vs. Peephole Parsing

Macro Processing When does it occur? –Before step 1. Lexical Analysis –Thus: 0. Macro Processing What is it? –Text substitution –Following precise rules In C, Macro Processing is done by the C Preprocessor –Takes c source file –Does macro processing –Passes results to C Compiler

Macro Processing? #define ONE 1 x = y + ONE; becomes x = y + 1; return DONE; remains unchanged

C Preprocessor in Action! /usr/lib/cpp gcc -E Both run c preprocessor sending output to stdout /usr/lib/cpp yourprogram.c > someFile.txt gcc -E yourprogram.c > someFile.txt

Macro Examples #include #include “defs.h” Looks in /usr/include for stdio.h inserts contents into source file Looks for defs.h in local directory and inserts contents into source file

Macro Examples #define EXIT_SUCCESS 0 Note: Not a constant const int foo = 42; Macro processing uses a symbol table technique

Symbol Table Used commonly in any translation operation Symbol NameSymbol Attributes footype, value, address, scope, lifetime, constant

Example #define EXIT_SUCCESS 0 return EXIT_SUCCESS;

Example #define EXIT_SUCCESS 0 return 0;

Why? EXIT_SUCCESS Meaningful self-documentation

Shooting oneself in the foot #define ONE 1 #define TWO ONE + ONE int x = ONE, y = TWO; becomes int x = 1, y = ONE + ONE; becomes int x = 1, y = 1 + 1;

Efficiency Clearly y = 2; is more efficient than y = 1 + 1;

WRONG!

When do things happen? Translation –“static” stuff Execution –“dynamic” stuff Preprocessor Here

More foot perforating int z = TWO * TWO; becomes int z = * 1 + 1; Probably not what was intended!!! Parentheses are your friend #define TWO (ONE + ONE) #define ONE (1) /* OVERKILL */

Parentheses ANSI standard guarantees 32 levels of parentheses nesting* *enough to satisfy Mr. Scheme: Kurt Eiselt

More shootin’ #define SQUARE(X) (X * X) int z = SQUARE(2); int z = SQUARE(x + y); Fix: #define SQUARE(X) ((X) * (X)) cuddling OK?

NOT! z = SQUARE(x++); becomes z = ((x++) * (x++)); What’s the correct answer? z = x 2 z = (x + 1) 2 z = x * (x + 1) z = (x + 1) * (x + 2)

NOT! z = SQUARE(x++); becomes z = ((x++) * (x++)); What’s the correct answer? z = x 2 /* maybe */ z = (x + 1) 2 /* probably not */ z = x * (x + 1) /* maybe */ z = (x + 1) * (x + 2) /* Probably not */

Another Possibility Make SQUARE a function int square(int x) { return x * x; } Now it’s a win! z = square(x); z = square(x+y); z = square(x++); All work properly Or is it? Any downside?

What happens? /* Macro */ SQUARE(x). SQUARE(x). SQUARE(x) /* Function */ square(x). square(x). square(x)

What happens? /* Macro */ ((x) * (x)). ((x) * (x)). ((x) * (x))

What happens? /* Macro */ ((x) * (x)). ((x) * (x)). ((x) * (x)) /* Function */ pass parameter(s) call function. pass parameter(s) call function. pass parameter(s) call function. square function return

What happens? /* Macro */ ((x) * (x)). ((x) * (x)). ((x) * (x)) /* Function */ pass parameter(s) call function. pass parameter(s) call function. pass parameter(s) call function. square function return

What happens? /* Macro */ ((x) * (x)). ((x) * (x)). ((x) * (x)) /* Function */ pass parameter(s) call function. pass parameter(s) call function. pass parameter(s) call function. square function return

What happens? /* Macro */ ((x) * (x)). ((x) * (x)). ((x) * (x)) /* Function */ pass parameter(s) call function. pass parameter(s) call function. pass parameter(s) call function. square function return

What happens? /* Macro */ ((x) * (x)). ((x) * (x)). ((x) * (x)) /* Function */ pass parameter(s) call function. pass parameter(s) call function. pass parameter(s) call function. square function return

What happens? /* Macro */ ((x) * (x)). ((x) * (x)). ((x) * (x)) /* Function */ pass parameter(s) call function. pass parameter(s) call function. pass parameter(s) call function. square function return

What happens? /* Macro */ ((x) * (x)). ((x) * (x)). ((x) * (x)) /* Function */ pass parameter(s) call function. pass parameter(s) call function. pass parameter(s) call function. square function return

Macros vs. Functions Macros –Text substitution at Translation (compile) time –May have problems: e.g. square(x++) –Will work with different types due to operator overloading floats, doubles, ints, … –Difficult to implement if complex –Macro optimizes for speed. Why? Functions –Separate piece of code –Overhead of passing arguments and returning results via stack –Fixes ambiguity problems: e.g. square(x + y) or(x++) –Function optimizes for space. Why?

Macros vs. Functions If the goal is not clearly optimization for speed or time but rather somewhere in-between it’s difficult to know exactly which choice is correct. In any event: Don’t try and outwit the compiler! A better algorithm is more of an improvement that trying to write tricky code!!!

Inline Can write inline int square(int x) { return x*x; } Now square(a++) will work correctly with macro like performance. Why use inline? Compiler will make decision for you if inline omitted gcc -finline-functions

Questions?