Unformatted text preview:

CS 61C Great Ideas in Computer Architecture Machine Structures Introduction to C Part I Instructors Randy H Katz David A Patterson http inst eecs Berkeley edu cs61c sp11 01 14 2019 Spring 2011 Lecture 3 1 Agenda Scheme vs Java vs C Administrivia Quick Start Introduction to C Technology Break Pointers Summary 01 14 2019 Spring 2011 Lecture 3 2 Levels of Representation Interpretation High Level Language Program e g C Compiler Assembly Language Program e g MIPS Assembler Machine Language Program MIPS temp v k v k v k 1 v k 1 temp lw lw sw sw t0 0 2 t1 4 2 t1 0 2 t0 4 2 0000 1010 1100 0101 1001 1111 0110 1000 1100 0101 1010 0000 We are here Anything can be represented as a number i e data or instructions 0110 1000 1111 1001 1010 0000 0101 1100 1111 1001 1000 0110 0101 1100 0000 1010 1000 0110 1001 1111 Machine Interpretation Hardware Architecture Description e g block diagrams Architecture Implementation Logic Circuit Description Circuit Schematic Diagrams Spring 2011 Lecture 3 01 14 2019 3 Agenda Scheme vs Java vs C Administrivia Quick Start Introduction to C Technology Break Pointers Summary 01 14 2019 Spring 2011 Lecture 3 4 Introduction to C the Universal Assembly Language Some C experience is required before CS61C C or Java OK Based on pre semester survey 83 already know JAVA 54 already know C 34 already know C 7 already know C About 10 have not taken 61B or equivalent If you have no experience in these languages then start early and ask a lot of questions in discussion 01 14 2019 Spring 2011 Lecture 3 5 Disclaimer You will not learn how to fully code in C in these lectures You ll still need your C reference for this course K R is a must have Check online for more sources JAVA in a Nutshell O Reilly Chapter 2 How Java Differs from C http oreilly com catalog javanut excerpt Brian Harvey s helpful transition notes On CS61C class website http inst eecs berkeley edu cs61c resources HarveyNotesC1 3 pdf Key C concepts Pointers Arrays Implications for Memory management 01 14 2019 Spring 2011 Lecture 3 6 C vs Java C Java Type of Language Function Oriented Object Oriented Programming Unit Function Class Abstract Data Type Compilation gcc hello c creates machine language code javac Hello java creates Java virtual machine language bytecode Execution a out loads and executes program java Hello interprets bytecode hello world public class HelloWorld include stdio h public static void int main void main String args printf Hello n System out printl Hello return 0 Storage Manual malloc free 01 14 2019 Automatic garbage collection Fall 2010 Lecture 1 From http www cs princeton edu introcs faq c2java html 7 Basic C Concepts Compiler Structs Creates useable programs from C source Kind of data that a variable contains The kind of data returned from a function Declare functions and variables in a separate file Groups of related values Enums Lists of predefined values Pointers Aliases to other variables Typed variables Typed functions Header files h These concepts distinguish C from other languages you may know 01 14 2019 Spring 2011 Lecture 3 8 Compilation Overview C compilers map C programs into architecture specific machine code string of 1s and 0s Unlike Java which converts to architecture independent bytecode Unlike most Scheme environments which interpret the code These differ mainly in exactly when your program is converted to low level machine instructions levels of interpretation For C generally a two part process of compiling c files to o files then linking the o files into executables Assembling is also done but is hidden i e done automatically by default 01 14 2019 Spring 2011 Lecture 3 9 Compilation Advantages Excellent run time performance generally much faster than Scheme or Java for comparable code because it optimizes for a given architecture Fair compilation time enhancements in compilation procedure Makefiles allow only modified files to be recompiled Why C we can write programs that allow us to exploit underlying features of the architecture memory management special instructions parallelism 01 14 2019 Spring 2011 Lecture 3 10 Compilation Disadvantages Compiled files including the executable are architecture specific depending on CPU type and the operating system Executable must be rebuilt on each new system I e porting your code to a new architecture Change Compile Run repeat iteration cycle can be slow during the development cycle 01 14 2019 Spring 2011 Lecture 3 11 Typed Variables in C int variable1 float variable2 char variable3 2 1 618 A You have to declare the type of data a variable will hold Types can t change Type int unsigned int float char double long 01 14 2019 Description integer numbers including negatives integer numbers no negatives floating point decimal numbers single text character or symbol greater precision FP number larger signed integer Spring 2011 Lecture 3 Examples 0 78 1400 0 46 900 0 0 1 618 1 4 a D 12 Typed Functions in C int numberOfPeople return 3 float dollarsAndCents return 10 33 char firstLetter return A 01 14 2019 You have to declare the type of data you plan to return from a function Return type can be any C variable type and is placed to the left of the function name You can also specify the return type as void Just think of this as saying that no value will be returned Also necessary to define types for values passed into a function Variables and functions MUST be defined before they are used Spring 2011 Lecture 3 13 Structs in C Structs are structured groups of variables e g typedef struct int lengthInSeconds int yearRecorded Song Song song1 song1 lengthInSeconds 213 song1 yearRecorded 1994 Song song2 song2 lengthInSeconds 248 song2 yearRecorded 1988 01 14 2019 Dot notation x y value Spring 2011 Lecture 3 14 Consts and Enums in C Constant is assigned a value once in the declaration value can t change until the program is restarted const float goldenRatio 1 618 const int daysInWeek 7 You can have a constant version of any of the standard C variable types Enums a group of related constants used to parameterize libraries 01 14 2019 Spring 2011 Lecture 3 15 Agenda Scheme vs Java vs C Administrivia Quick Start Introduction to C Technology Break Pointers 01 14 2019 Spring 2011 Lecture 3 16 Administrivia We promised you that CS61c would be relentless This week Lab 2 HW 2 Lab 2 Amazon EC2 will soon be posted HW 2 will soon be posted TAs have their office hours late in the week don t wait for last minute to get questions answered on HWs and Labs Take


View Full Document

Berkeley COMPSCI 61C - Introduction to C (Part I)

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
Loading Unlocking...
Login

Join to view Introduction to C (Part I) 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 Introduction to C (Part I) 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?