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

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

Transcript of The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J....

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

The preprocessor and the compilation process

COP3275 – PROGRAMMING USING CDIEGO J. RIVERA-GUTIERREZ

Page 2: 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?

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

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)

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

The compilation process

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

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…

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

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

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

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

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

#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).

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

#define

• Defines a constant.

#define <name> <value>

• 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.

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

Other functions we will cover

• #ifdef

• #endif

• #else

• #ifndef

• #if

• #elif

• __VA_ARGS__

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

Let’s do some examples

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

More on the preprocessor

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

Administrative stuff

• Homework #5 is due tonight.• Questions?

• We have 3 quizzes left• Today• July 31st in class• August 7th – take home will be assigned on August 5th. Due August 7th

at midnight.• Will have a chance for 5 extra points.

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

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

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

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

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

#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

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

#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.

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

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

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

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.