DOC PREVIEW
Berkeley COMPSCI 61C - Lecture Notes

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

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 11 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 11 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 11 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 11 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 11 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 61C L03 Introduction to C (1) Wawrzynek Spring 2006 © UCB1/23/2006John Wawrzynek(www.cs.berkeley.edu/~johnw)www-inst.eecs.berkeley.edu/~cs61c/CS61C – Machine StructuresLecture 3 – Introduction to the C Programming LanguageCS 61C L03 Introduction to C (2) Wawrzynek Spring 2006 © UCBAdministrivia : Near term°Get cardkeys from CS main officeSoda Hall 3rd floor.°Reading for this week:• K&R Ch 1-4 (today, Ch 5-6 (W, F)°HW• HW1 due Wednesday 11:59pm.• HW2 will be posted Wednesday.°Project 1 - C Programming• Goes online tomorrow AM• Due Monday 2/6 (2 weeks from today)CS 61C L03 Introduction to C (3) Wawrzynek Spring 2006 © UCBIntroduction to CWhy learn C?CS 61C L03 Introduction to C (4) Wawrzynek Spring 2006 © UCBDisclaimer°Important: You will not learn how tofully code in C in these lectures!You’ll still need your C reference forthis course.• K&R is a great reference.- But… check online for more sources.• “JAVA in a Nutshell” – O’Reilly.- Chapter 2, “How Java Differs from C”.• Brian Harvey’s course notes.- On class website.CS 61C L03 Introduction to C (5) Wawrzynek Spring 2006 © UCBCompilation : OverviewC compilers take C and convert it intoan architecture specific machine code(string of 1s and 0s).• Unlike Java which converts toarchitecture independent “bytecodes”.• Unlike most Scheme environments whichinterpret the code.(These differ mainly in when your programis converted to machine instructions.)For C generally a 2 part process ofcompiling .c files to .o files, then linkingthe .o files into executablesCS 61C L03 Introduction to C (6) Wawrzynek Spring 2006 © UCBCompilation : characteristics°Great run-time performance: generallymuch faster than Scheme or Java forcomparable code (because itoptimizes for a given architecture)°OK compilation time: enhancementsin compilation procedure (Makefiles)allow only modified files to berecompiledCS 61C L03 Introduction to C (7) Wawrzynek Spring 2006 © UCBCompilation : Disadvantages°All compiled files (including theexecutable) are architecture specific,depending on both the CPU type andthe operating system.°Executable must be rebuilt on eachnew system.• Called “porting your code” to a newarchitecture.°The “change→compile→run [repeat]”iteration cycle is slowCS 61C L03 Introduction to C (8) Wawrzynek Spring 2006 © UCBC vs. Java™ Overview (1/2)Java• Object-oriented(OOP)• “Methods”• Class libraries ofdata structures• AutomaticmemorymanagementC• No built-in objectabstraction. Dataseparate frommethods.• “Functions”• C libraries arelower-level• Manualmemorymanagement• PointersCS 61C L03 Introduction to C (9) Wawrzynek Spring 2006 © UCBC vs. Java™ Overview (2/2)Java• High memoryoverhead fromclass libraries• Relatively Slow• Arrays initializeto zero• Syntax: /* comment */// commentSystem.out.printC• Low memoryoverhead• Relatively Fast• Arrays initializeto garbage• Syntax:/* comment */printfNewer C compilers allow Java style comments as well!CS 61C L03 Introduction to C (10) Wawrzynek Spring 2006 © UCBC Syntax: Variable Declarations°Very similar to Java, but with a few minorbut important differences°All variable declarations mustgo before they are used(at the beginning of the block).°A variable may be initialized in itsdeclaration.°Examples of declarations:• correct: {int a = 0, b = 10;...• incorrect: for (int i = 0; i < 10; i++)C compiler now allow this in the case of “for” loops.CS 61C L03 Introduction to C (11) Wawrzynek Spring 2006 © UCBC Syntax: True or False?°What evaluates to FALSE in C?• 0 (integer)• NULL (pointer: more on this later)• no such thing as a Boolean°What evaluates to TRUE in C?• everything else…• (same idea as in scheme: only #f isfalse, everything else is true!)CS 61C L03 Introduction to C (12) Wawrzynek Spring 2006 © UCBC syntax : flow control° Within a function, remarkably close toJava constructs in methods (shows itslegacy) in terms of flow control•if-else•switch•while and for•do-whileCS 61C L03 Introduction to C (13) Wawrzynek Spring 2006 © UCBC Syntax: main°To get the main function to acceptarguments, use this:int main (int argc, char *argv[])°What does this mean?•argc will contain the number of stringson the command line (the executablecounts as one, plus one for eachargument).- Example: unix% sort myFile•argv is a pointer to an array containingthe arguments as strings (more onpointers later).CS 61C L03 Introduction to C (14) Wawrzynek Spring 2006 © UCBAddress vs. Value°Consider memory to be a single hugearray:• Each cell of the array has an addressassociated with it.• Each cell also stores some value• Do you think they use signed orunsigned numbers? Negative address?!°Don’t confuse the address referring toa memory location with the valuestored in that location.23 42 ... ...101 102 103 104 105 ...CS 61C L03 Introduction to C (15) Wawrzynek Spring 2006 © UCBPointers°An address refers to a particularmemory location. In other words, itpoints to a memory location.°Pointer: A variable that contains theaddress of another variable.23 42 ... ...101 102 103 104 105 ...xyLocation (address)namep104CS 61C L03 Introduction to C (16) Wawrzynek Spring 2006 © UCBPointers°How to create a pointer:& operator: get address of a variableint *p, x;p ? x ?x = 3;p ? x 3p =&x;p x 3°How get a value pointed to? * “dereference operator”: get value pointed toprintf(“p points to %d\n”,*p);Note the “*” gets used2 different ways inthis example. In thedeclaration to indicatethat p is going to be apointer, and in theprintf to get thevalue pointed to by p.CS 61C L03 Introduction to C (17) Wawrzynek Spring 2006 © UCBPointers°How to change a variable pointed to?• Use dereference * operator on left of =p x 5*p = 5;p x 3CS 61C L03 Introduction to C (18) Wawrzynek Spring 2006 © UCBPointers and Parameter Passing°Java and C pass a parameter “by value”• procedure/function gets a copy of theparameter, so changing the copy cannotchange the original void addOne (int x) { x = x + 1;} int y = 3; addOne(y);y is still = 3CS 61C L03 Introduction to C (19) Wawrzynek Spring 2006 © UCBPointers and Parameter Passing°How to get a function to change a value? void addOne (int *p) {*p = *p + 1;} int y = 3; addOne(&y);y is now = 4CS 61C L03 Introduction to C (20) Wawrzynek Spring 2006 © UCBPointers°Of course pointers are used to point toany data type (int, char, a


View Full Document

Berkeley COMPSCI 61C - Lecture Notes

Documents in this Course
SIMD II

SIMD II

8 pages

Midterm

Midterm

7 pages

Lecture 7

Lecture 7

31 pages

Caches

Caches

7 pages

Lecture 9

Lecture 9

24 pages

Lecture 1

Lecture 1

28 pages

Lecture 2

Lecture 2

25 pages

VM II

VM II

4 pages

Midterm

Midterm

10 pages

Load more
Download Lecture Notes
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 Notes 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 Notes 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?