Unformatted text preview:

1CMSC 212 – S07 (lect 6)Announcementsz Program #2– Due two weeks from todayz Reading– Chapter 15 (today)• skip 15.10.1 and 15.10.2 (scanf)– Chapter 112CMSC 212 – S07 (lect 6)Project Notesz Start Early! - It's longer than P1– There are 12 total tests (5 public, 7 release)z Helpful functions– int strncpy(char *s1, char *s2, int n);– int atoi(char *);– int strcspn(char *s1, char *s2);• Return index of last character in s1 that does not contain any characters from s2z Suggestions–Start simple• write command line argument code• write code to read input file• Test and add code in steps3CMSC 212 – S07 (lect 6)I/Oz void perror(char *message);– print out the most recent error encountered by a library– prints message: <error description>–Example: • We tried to do something with the file foo.c and it didn't exist• perror("foo.c") would print foo.c: File Not Foundz Terminating execution– void exit(int status);– ends the program at that spot (no need to return from main)– status is available to other programs to indicate why the program ended• exit (0) is typical for no error• exit(-1) or some other negative value for an error4CMSC 212 – S07 (lect 6)Printf (the full story)z int fprintf(FILE *stream, char *format, …);– sends output to streamz printf(char *format, …);– sends output to stdoutz snprintf(char *buffer, int limit, char *format, …);– sends output the the character buffer– prints at most limit-1 characters to buffer• adds null to end of stringz never! use sprintf (due to buffer overflow)5CMSC 212 – S07 (lect 6)Printf format codesz c print argument as unsigned characterz d print as decimal integerz u print as unsigned integerz x print as hex (use X for capital A-F in format)z e print in exponent form (6.02300e23)z f print in floating point formatz s print as a string (null terminated character array)z % print a % (so %% prints one %)6CMSC 212 – S07 (lect 6)Printf – Flags and Field Widthsz Flags– - left justify the field (default is right)– + for signed numbers force the + to be printed–#• For x and X ensures a leading 0x or 0Xz Precision – How many characters will be printed– Format is x.y• For int types – x is total width and y is numeric digits– Leading 0’s are added to bring width up to y• For floating points numbers – x is total width and y is digits right of decimal point7CMSC 212 – S07 (lect 6)Format code examplesABCDEFGHABC..A….%-5sABCDE..ABC….A%5.5sABCDEABCA%.5sABCDEFGH..ABC….A%5sABCDEFGHABCA%sABCDEFGHABCAFormat Code+123456789+12345-12+1%+d12345678912345-0120001%04d12345678912345-12.1…%-4d123456789.12345.-0012..0001%6.4d12345678912345-00120001%.4d123456789.12345…-12…..1%6d12345678912345-121%d12345678912345-121Format8CMSC 212 – S07 (lect 6)Binary I/Oz Write out data structures in raw format– Tends to be non-portablez size_t fread(void *buffer, size_t size, size_t count, FILE *stream);– Read into buffer, count items of size size from stream– Returns the number of items read– ret = fread(mystuff, sizeof(struct foo), 42, stdin);z size_t fwrite(void *buffer , size_t size, size_t count, FILE *stream);– Write to stream, count items of size size starting at buffer– Return the number of items written9CMSC 212 – S07 (lect 6)Other FILE Operationsz int fflush(FILE *fp);– Write any buffered output to the destinationz long ftell(FILE *stream);– Return the current position (offset) of the stream– Return value is in bytesz int fseek(FILE *stream, long offset, int from);– Change the position of the stream– Where depends on value of from• SEEK_SET - offset bytes from start of file• SEEK_CUR - offset bytes from the current position• SEEK_END - offset bytes from the end of the file• Last two may be positive or negative10CMSC 212 – S07 (lect 6)Misc. FILE Functionsz Error and Status– int feof(FILE *stream);• Returns true if currently at end of file– int ferror(FILE *stream);• Returns true if any error condition is true– void clearerr(FILE *stream);• Reset the error indication for the filez Temporary Files– Sometimes need a file that is temporary• But it needs to be unique– FILE *tmpfile(void);• Create a new file that, it will be deleted on close11CMSC 212 – S07 (lect 6)Make: A Tool to Compile Programsz Problem: Compiling programs is tediousz Solution: Automate it!z Done in Unix by the make command– Uses a file called Makefile•A makefile is a file (script) containing :– Project structure (files, dependencies)– Instructions for files creationz Make is not limited to C programsAdapted from: www.cs.tau.ac.il/~dcor/Software1/Makefile.ppt12CMSC 212 – S07 (lect 6)Describing Files to Makez Makefile contains project dependencies– represented as a DAG (= Directed Acyclic Graph) z Example:– Program contains 3 files– main.c., sum.c, sum.h– sum.h included in both .c files– Executable should be the file sumsum (exe)sum.omain.osum.csum.hmain.c13CMSC 212 – S07 (lect 6)Makefile for Previous Examplesum: main.o sum.ogcc –o sum main.o sum.omain.o: main.c sum.hgcc –c main.c sum.o: sum.c sum.hgcc –c sum.c z Notes About Makefiles:– Target is first item in a dependency– Items after “:” required to build target– Actions must have a tab at start– First target in file is the default rootTarget DependencyTabAction14CMSC 212 – S07 (lect 6)Make’s Operationz Project dependencies tree is constructedz A target is out of date when one of its dependencies is newer than itself.z If out of date, recreate the target– Perform action specified– Done on the way up the tree. z Notes:– Ensures minimum compilation, with proper project structure– Avoid writing something like:prog: main.c sum1.c sum2.cgcc –o prog main.c sum1.c sum2.c• requires full compilation when something changes15CMSC 212 – S07 (lect 6)Additional Make Featuresz Macros– <variable> = <value>–Example:CC = gccCFLAGS = -g -Wallfoo.o: foo.c$(CC) $(CFLAGS) –c foo.cz Default Action– $(CC) –c $(CFLAGS)z Actions can sometimes delete files not create them–Example:Clean:rm *.o16CMSC 212 – S07 (lect 6)More Complex Makefileall: intCount tableclean:rm -f *.o *.bb *.bbg *.da *.pngintCount: intCount.o$(CC) -o intCount intCount.otable: table.o main.o hashTable.o$(CC) -o table table.o main.o hashTable.otable.o: table.c table.hhashTable.o: hashTable.c table.hmain.o: main.c table.hCC = gccCFLAGS = -g -WallTwo top-level


View Full Document

UMD CMSC 212 - Lecture Slides

Download Lecture Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?