Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out...

18
Announcements Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel there was a mistake, email your instructor (that’s me ) Let’s have NO formatting mistakes for Assignment 2! Make sure you name the files EXACTLY as in the specs. (remember Unix cares about case so isFib.c is NOT the same as isfib.c) Use the example programs and diff to 1

description

Example Makefile mayan : mayan.c gcc –Wall mayan.c –o mayan What it means 1.The file mayan is built from the file mayan.c 2.To build mayan from mayan.c, execute this command 3

Transcript of Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out...

Page 1: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

Announcements

• Assignment 1 will be regraded for all who’s score (not percentage) is less than 6 (out of 65).

• If your score is 6 or higher, but you feel there was a mistake, email your instructor (that’s me )

• Let’s have NO formatting mistakes for Assignment 2!• Make sure you name the files EXACTLY as in the

specs. (remember Unix cares about case so isFib.c is NOT the same as isfib.c)

• Use the example programs and diff to make sure your program is correct.

1

Page 2: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

What is “make”?

• make is a Unix tool that simplifies the task of compiling large and complex software projects

– the programmer creates a Makefile that speficies:• which files depend on which other files• what commands to run when a file changes and needs to be

(re)compiled– make reads Makefile and runs commands as necessary

• not such a big deal for small single-source-file programs• very helpful when a large number of files involved• can be used for many tasks that involve running some commands

on files that have changed

2

Page 3: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

Example

Makefilemayan : mayan.c gcc –Wall mayan.c –o mayan

What it means1. The file mayan is built from

the file mayan.c2. To build mayan from mayan.c,

execute this command

3

Page 4: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

Makefile structure: rules

Makefilemayan : mayan.c gcc –Wall mayan.c –o mayan

4

“target”

file(s) the target depends on ( ≥ 0 )(“prerequisites”)

if any of these files is changed, the target must be recompiled.

command ( ≥ 0 ) to execute if target is out of date w.r.t. the files it depends on

“rule”

Page 5: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

How make works

Makefile

target : prerequisite1 … prerequisiten

command

5

1. if target does not exist:– run command

2. else: if target is older than any of its prerequisites:– run command

3. else: /* (target is up to date) */– do nothing

Simplified Behavior of make

Page 6: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

Example

6

Page 7: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

Multiple Targets

Makefile

target1 : prerequisite1 … prerequisiten

command1

target2 : prerequisite1 … prerequisitem

command2

7

1. You can specify the target:make target2

2. If you don’t specify a target, the first target is used.

Which target gets “made”

Page 8: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

What can commands be?

Makefile

target : prerequisite1 … prerequisiten

ls -l

8

1. The command doesn't have to be gcc, it can be any Unix command.

2. Here there is a rather silly Makefile that runs ls -l when target doesn't exist or has date older than a prerequisite

3. Notice ls doesn't update target. What happens then if make is run twice in a row?

Page 9: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

clean

Makefile

isFib: isFib.c gcc -Wall -o isFib isFib.c

clean: rm -f isFib

9

1. Often a makefile will include a "clean" object to remove files associated with the project

2. Here typing make clean will cause the executable to be deleted.

3. Notice that if there is no file named "clean" in the current directory, the rm command will run everytime you typemake clean

Page 10: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

Characters and strings

• A character is of type char (signed or unsigned)– C99 provides support for “wide characters”

• strings :– an array of characters – terminated by a 0 character (“NUL” : written ‘\0’)– Not a predefined type in C; string operations are provided

through libraries

C arrays don’t do bound-checking– careless programming can give rise to memory-corruption

errors

10

Page 11: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

Example of string manipulation

11

declares an array of chars

the input analog to printf: “read a value into str”(%s = “string”)

print out a string (%s)

Page 12: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

More strings…

12

waits for input (from stdin)

typed in on keyboard: stdin

program output (stdout)

end-of-file indicated by typing Ctrl-D

Page 13: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

More strings…

13

Oops!

this means the program tried to access an illegal memory address

Page 14: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

More detail

• So “strings” are arrays of characters (that end with a NULL (‘\0’))

• But what is a character? • A character is a byte of data. • This can be viewed as a (small) number or as a

symbol from the ASCII table.• To specify a character in C use single quotes:

char c;c = ‘b’;

• Let’s go to a terminal and experiment

14

Page 15: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

• So even though characters can be thought of as letters, digits, or symbols, you can also use addition, subtraction, multiplication, etc on them.

• You can also compare them: ‘A’ < ‘G’ evaluates to true.

• The uppercase letters and lower case letters run in a sequence.

• i.e.: ‘c’ + 1 == ‘d’

15

Page 16: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

• How can we use this to write a test if a character c is a capitol letter?

16

Page 17: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

• How can we use this to write a test if a character c is a upper case letter?

if (c >= ‘A’ && c <= ‘Z’) {// if c is an uppercase letter then we go here}

• Let’s go to the terminal and write a function to convert all the lowercase letters of a string to upper case.

17

Page 18: Announcements Assignment 1 will be regraded for all whos score (not percentage) is less than 6 (out of 65). If your score is 6 or higher, but you feel.

The completed program

18