Unformatted text preview:

1CMSC 212 – S05 (lect 3)Announcementsz Program #1A– due next Thursdayz Email– please send email to course staff from UMD accounts– email from hotmail, yahoo, aol, etc. will likely be auto deletedz Programming Environment– Must use Linux, disregard suggestions from section about Eclipse or Visual studioz Reading– Chapter 3 & 4– Chapter 7 (Thursday)2CMSC 212 – S05 (lect 3)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.ppt3CMSC 212 – S05 (lect 3)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.c4CMSC 212 – S05 (lect 3)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 DependencyTabAction5CMSC 212 – S05 (lect 3)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–oprogmain.c sum1.c sum2.c• requires full compilation when something changes6CMSC 212 – S05 (lect 3)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 *.o7CMSC 212 – S05 (lect 3)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 clean”8CMSC 212 – S05 (lect 3)Data Types in Cz Integer Family– char typically 8 bits– short typically 16 bits– int typically 32 or 64 bits– long typically 32 or 64 bits– long long typically 64 bits– All are signed by default, but can be made unsigned• unsigned int (typically, 0 to 4,294,967,295)– Literals• Decimal (255), Hex (0xff), signed (-255)• Character (‘a’, ‘\n’)– Don’t be stingy with size, when in doubt use a larger sizez Floating Point– float, double, long double– Literals (3.14159, 1E10, 25., 6.023e23)9CMSC 212 – S05 (lect 3)Data Types in C (cont.)z String Literals– “a long dull string”, “\n”, “”z Enumerated Types– Declaring an enumerated type:• enum operatorType { plusOperator, minusOperator };– Declaring a variable of an enumerated type:• enum operatorType currentOperator;– Using an enumerated type in an expression:• currentOperator = plusOperator;– Can control values of enumerated type:• enum operatorType { plusOperator=3, minusOperator =8};10CMSC 212 – S05 (lect 3)Variable Declarationsz <type> <variable1>, <variable2>, … ;– int a, b, c;z Variables names may not be reserved words– Must start with A-Z,a-z,_– May contain numbers or “_”• aValid_Variable_Name• aValid_Variable_Name_2z May initialize variables as part of declaration– int a = 4;z Implicit Declarations (Book section 3.2.4)– Skip section book and don’t ever use11CMSC 212 – S05 (lect 3)Simple Arraysz Array dimensions must be known at compile timez Array Declaration– Int a[10]; /* array of 10 elements */z Accessing array elements– Arrays start from 0 and go to n-1 elements– Compiler will let programmer access invalid elements• Can lead to many problems that are hard to find– Examples of array references•A[0]•A[i]•A[i+3]12CMSC 212 – S05 (lect 3)Typedefz Sometimes you want to name your own type– Makes program easier to read– Makes it easier to change types laterz typedef double dollars;13CMSC 212 – S05 (lect 3)Variable Scopez File Scope– Entire file– Not declared inside any functionz Block Scope– Before an executable statement in a block of code { .. }z Repeating same variables in nested scopevoid main() {int a;{ int a; a = 3; }}– Should be avoided!!14CMSC 212 – S05 (lect 3)Storage and Linkagez Linkage– Determines how multi-file visibility issues are resolved– extern - There is only one instance of this variable– static - This variable is private to this filefoo.c:static int a;bar.c:static in a;z Storage Classes– Default is automatic: • one variable instance per function instance• lifetime limited to while function is active – running or in called subroutine– Static changes default• one variable instance• lifetime is the entire program15CMSC 212 – S05 (lect 3)Storage Class and Linkage Exampleextern int b;static int c;Int func1(int e){static int longLived;int


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?