DOC PREVIEW
UMD CMSC 212 - Lecture Slides

This preview shows page 1-2-3-4-5 out of 15 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 15 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC 212 – S07 (lect 6)AnnouncementsProgram #2– Due two weeks from todayReading– Chapter 15 (today)• skip 15.10.1 and 15.10.2 (scanf)– Chapter 112CMSC 212 – S07 (lect 6)Project NotesStart Early! - It's longer than P1Helpful functions– int strncpy(char *s1, char *s2, int max);– size_t strspn(const char *, const char *);– size_t strcspn(const char *, const char *);– int isalnum(char);– void perror(char *message);– void exit(int status);Suggestions– Start simple• write command line argument code• write code to read input file• write code to parse and/or modify input line as needed• Test and add code in steps3CMSC 212 – S07 (lect 6)Printf (the rest of the story)int fprintf(FILE *stream, char *format, …);– sends output to streamprintf(char *format, …);– sends output to stdout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 stringnever! use sprintf (due to buffer overflow)4CMSC 212 – S07 (lect 6)Printf format codesc print argument as unsigned characterd print as decimal integeru print as unsigned integerx print as hex (use X for capital A-F in format)e print in exponent form (6.02300e23)f print in floating point formats print as a string (null terminated character array)% print a % (so %% prints one %)5CMSC 212 – S07 (lect 6)Printf – Flags and Field Widths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 0X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 point6CMSC 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-121Format7CMSC 212 – S07 (lect 6)Binary I/OWrite out data structures in raw format– Tends to be non-portable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);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 written8CMSC 212 – S07 (lect 6)Other FILE Operationsint fflush(FILE *fp);– Write any buffered output to the destinationlong ftell(FILE *stream);– Return the current position (offset) of the stream– Return value is in bytes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 negative9CMSC 212 – S07 (lect 6)Misc. FILE Functions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 file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 close10CMSC 212 – S07 (lect 6)Make: A Tool to Compile ProgramsProblem: Compiling programs is tediousSolution: Automate it!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 creationMake is not limited to C programsAdapted from: www.cs.tau.ac.il/~dcor/Software1/Makefile.ppt11CMSC 212 – S07 (lect 6)Describing Files to MakeMakefile contains project dependencies– represented as a DAG (= Directed Acyclic Graph) 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.c12CMSC 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  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 DependencyTabAction13CMSC 212 – S07 (lect 6)Make’s OperationProject dependencies tree is constructedA target is out of date when one of its dependencies is newer than itself.If out of date, recreate the target– Perform action specified– Done on the way up the tree. 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 changes14CMSC 212 – S07 (lect 6)Additional Make FeaturesMacros– <variable> = <value>– Example:CC = gccCFLAGS = -g -WallFoo.o: foo.c$(CC) $(CFLAGS) –c foo.cDefault Action– $(CC) –c $(CFLAGS)Actions can sometimes delete files not create them– Example:Clean:rm *.o15CMSC 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 programsLink together several filesUses Default RuleMacro DefinitionsNot part of main treebuilt by typing “make


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?