CIS 190: C/C++ Programming
Lecture 4
Assorted Topics
(and More on Pointers)
1 Outline
• Makefiles
• File I/O
• Command Line Arguments
• Random Numbers
• Re-Covering Pointers
• Memory and Functions
• Homework
2 Makefiles
• list of rules you can call from the terminal
–make ruleTwo will call the ruleTwo
–make will call first rule in the file
• basic formatting
– use at line beginning to denote comments
– must use tab character, not 8 spaces
3 Rule Construction
target: dependencies (optional)
command
another command (optional)
• target (rule name)
• dependency (right side of colon)
• command (explicit commands)
4 Creating a Rule
• let’s create a rule to compile and link the files
for Homework 4A:
hw4a.c
karaoke.c
karaoke.h
• what commands will let us do this?
5 Creating a Rule
we need to, in order:
1. separately compile hw4a.c
2. separately compile karaoke.c
3. link hw4a.o and karaoke.o together
6 Creating a Rule
1. separately compile hw4a.c
• we’ll make a rule called hw4a.o
• what command would we run in the terminal?
• what files does it need to work?
7 Creating a Rule
1. separately compile hw4a.c
• we’ll make a rule called hw4a.o
• what command would we run in the terminal?
• what files does it need to work?
– hw4a.c
– so it’s dependent on hw4a.c
8 Creating a Rule
2. separately compile karaoke.c
• we’ll call this rule karaoke.o
• what command will compile karaoke.c?
• what files does it need to work?
9 Creating a Rule
2. separately compile karaoke.c
• we’ll call this rule karaoke.o
• what command will compile karaoke.c?
• what files does it need to work?
– karaoke.c
– so it’s dependent on karaoke.c
10 Creating a Rule
3. link hw4a.o and karaoke.o together
• we’ll call this rule hw4a
• what command will link the files together?
• what files does it depend on?
11 Creating a Rule
3. link hw4a.o and karaoke.o together
• we’ll call this rule hw4a
• what command will link the files together?
• what files does it depend on?
– hw4a.o
– karaoke.o
– so it’s dependent on both of these files
12 Other Common Rules
• a rule to remove .o and executable files
clean:
rm –f .o hw4a
• a rule to remove garbage files
cleaner:
rm –f
• a rule to run both
cleanest: clean cleaner
13
Why Use Makefiles
• makes compiling, linking, executing, etc
– easier
– quicker
– less prone to human error
• allows use to create and run helper rules
– clean up unneeded files (like hw2.c or trains.o)
– open files for editing
14 Makefiles and Beyond
• there’s much more you can do with Makefiles
– variables
– conditionals
– system configuration
– phony targets
• more information available here
http://www.chemie.fu-berlin.de/chemnet/use
/info/make/make_toc.html
15 Outline
• Makefiles
• File I/O
• Command Line Arguments
• Random Numbers
• Re-Covering Pointers
• Memory and Functions
• Homework
16 Input and Output
• printf
– stdout
– output written to the terminal
• scanf
– stdin
– input read in from user
• redirection
– executable input.txt output.txt
17 FILE I/O Basics
• allow us to read in from and print out to files
– instead of from and to the terminal
• use a file pointer (FILE) to manage
the file(s) we want to be handling
• naming conventions:
FILE ofp; / output file pointer /
FILE ifp; / input file pointer /
18
Opening a File
FILE fopen (filename, mode);
• fopen() returns a FILE pointer
– hopefully to a successfully opened file
• filename is a string
• mode is single-character string
19 FILE I/O Reading and Writing
ifp = fopen(“input.txt”, “r”);
• opens input.txt for reading
– file must already exist
20
Advise:Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.