The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.

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)
Using MyMathLab Features You must already be registered or enrolled in a current class.
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.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
CSE 115 Week 3 January 28 – February 1, Monday Announcements Software Installation Fest: 2/5 and 2/6 4pm – 7pm in Baldy 21 Software Installation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
Using MyMathLab Features You must already be registered or enrolled in a current MyMathLab class in order to use MyMathLab. If you are not registered or.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Introduction to VBA. This is not Introduction to Excel We’re going to assume you have a basic level of familiarity with Excel If you don’t, or you need.
Pointers 2 COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
 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.
An Introduction to Textual Programming
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Files COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
BIT 115: Introduction To Programming Instructor: Mike Panitz
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
Multiple Files and beyond COP 3275 – PROGRAMMING USING C.
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
COP Structures Instructor: Diego Rivera-Gutierrez I’m back baby!
COP 3275 COMPUTER PROGRAMMING USING C Instructor: Diego Rivera-Gutierrez
Using MyMathLab Features of MyMathLab You must already be registered or enrolled in a current MyMathLab class in order to use MyMathLab. If you are not.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Application Day: Branching Adventure.  Quiz  Let's look at the schedule  Branching Adventure - explanation  Branching Adventure – work time 2.
COP 3275 – Iteration and loops Instructor: Diego Rivera-Gutierrez.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Pointers COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Linked List (II) CGS 3460, Lecture 37 Apr 12, 2006 Hen-I Yang.
COP 3275 – Character Strings Instructor: Diego Rivera-Gutierrez.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
 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.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
We’ll be spending a few minutes talking about Quiz 2 on Sections that you’ll be taking the next class session, before you work on Practice Quiz.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Problem Session 4 Header Files and Unix. Where are they? You may need to examine the header file to determine what the data structures and functions provided.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 13 – C: The Rest of the Preprocessor.
Programs – Preprocessing, Compilation and Linking
13 C Preprocessor.
C – Multi-file development and make
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
Using MyMathLab Features
Pre-processor Directives
C Preprocessor(CPP).
Using MyMathLab Features
Due Next Monday Assignment 1 Start early
Using CourseCompass Features
C Preprocessor Seema Chandak.
C Programming Language
Week 2 - Friday CS222.
CSE 303 Concepts and Tools for Software Development
Presentation transcript:

The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ

Administrative stuff Homework #4 grades were released If you got a 20 with a comment about your code not compiling on this or any other homework: you NEED to go to office hours!! Seriously, take a look at Canvas, make sure your grades reflect your effort. We are 2 weeks out of the end of the summer session. Don’t leave this to the last second. Homework #5 Due Friday at 11:59pm Questions?

Quiz on Friday One theory question about files (explain one of the functions we covered) One theory question on the preprocessor (to be covered today) Write a function that does something regarding files. Understand what a short piece of code using the preprocessor would do (to be covered today)

The compilation process

How does the compilation process looks like? source.c a.out (Binary file) gcc source.c -o a.out This was an oversimplification of the process…

This is actually a 3 stage process The first step is actually done by a subprogram of gcc which is called the preprocessor. (Which we will cover today) The second step is the actual compilation. The thirdstep is called “linking” which we will cover next Friday. source.c Preprocessor Unlinked Machine Code “Preprocessed” Source Compiler a.out (Binary file) Linker

Preprocessor The preprocessor handles mostly text changes You can almost (almost) think about it as a find and replace. Anything that starts with a # is a preprocessor directive. We have seen some in the past #include #define

#include We will talk more about include later. It has a lot to do with linking. But in general it brings another file and essentially copies it to your program. We will talk about how those files we’ve included look like later They are essentially a bunch of function declarations (not definitions).

#define Defines a constant. #define Usually names are put in all upper case (by convention, but not necessary) I used it for ROWS and COLS in my HW3 and HW4 solutions. What happens is the preprocessor does a find and replace. #define ROWS 9 It can also do computations It can have new lines (\) It can also receive parameters and do some cool things.

Other functions we will cover #ifdef #endif #else #ifndef #if #elif __VA_ARGS__

Let’s do some examples

More on the preprocessor

Administrative stuff Homework #5 is due tonight. Questions? We have 3 quizzes left Today July 31 st in class August 7 th – take home will be assigned on August 5 th. Due August 7 th at midnight. Will have a chance for 5 extra points.

Homework #6 Assignment will be posted tonight. Hangman Read a dictionary of words from a file, set from console. Receive seed from console. Allow users to guess letters and play. Upper case and lower case letters will be treated the same Format for the “ASCII art” will be provided. You’ll need to be able to print a alphabetically sorted version of the dictionary. You’ll need to organize your code in three files: One for game play functions One for handling the dictionary One with your main function

Preprocessor Last Wednesday we talked about #define Without With one parameter With newlines (\) Let’s actually make one version with two parameters. And one with variable parameters (__VA_ARGS__) #if #elif #else #endif

#ifdef and #ifndef This two are preprocessor checks to check if a macro is already defined Assume I say: #define DIEGO (notice that the meaning is optional, I can define a macro with no value) If I wanted to know if that is defined #ifdef DIEGO #endif

#ifdef and #ifndef Similarly if I wanted to check if something is not defined. #ifndef DIEGO #endif This is the actual if we use for operating systems.

If for detecting operating system _WIN32 Any modern Windows OS _WIN64 64bit Windows OS (almost all Windows versions in the last 4 years, but not earlier) __APPLE__ Mac systems __linux Most Linux distribution __unix Other Unix based operating systemss

Redifining a value What happens if I want to change the value of a macro We could write another #define, but does that work? I believe this is a warning (but let’s test it) Actually the correct way would undefining the macro and then defining it again #undef NULL This is generally not something you want. Treating macros as variables is VERY dangerous, because it happens during compilation time, but it is possible. Remember macros do mostly text replacement.