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

This preview shows page 1-2-3-19-20-39-40-41 out of 41 pages.

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

Unformatted text preview:

Slide 1AgendaLevels of Representation/InterpretationAgendaIntroduction to C “the Universal Assembly Language”DisclaimerC vs. JavaBasic C ConceptsCompilation: OverviewCompilation: AdvantagesCompilation: DisadvantagesTyped Variables in CTyped Functions in CStructs in CConsts and Enums in CAgendaAdministriviaAgendaA First C Program: Hello WorldA Second C Program: Compute Table of SinesSecond C Program Sample OutputC Syntax: mainC Syntax: Variable DeclarationsC Syntax : Flow Control (1/2)C Syntax : Flow Control (2/2)C Syntax: True or FalseAgendaAgendaAddress vs. ValuePointersPointersPointersPointers and Parameter PassingPointers and Parameter PassingPointersPeer Instruction QuestionMore C Pointer DangersPointers in CWhy Pointers in C?FYI—Update to ANSI CAnd In Conclusion, …CS 61C: Great Ideas in Computer Architecture (Machine Structures)Introduction to C (Part I)Instructors:Randy H. KatzDavid A. Pattersonhttp://inst.eecs.Berkeley.edu/~cs61c/sp111Spring 2011 -- Lecture #301/14/2019Agenda•Scheme vs. Java vs. C•Administrivia•Quick Start Introduction to C•Technology Break•Pointers•Summary01/14/2019 Spring 2011 -- Lecture #3 2Levels of Representation/Interpretationlw $t0, 0($2)lw $t1, 4($2)sw $t1, 0($2)sw $t0, 4($2)High Level LanguageProgram (e.g., C)Assembly Language Program (e.g., MIPS)Machine Language Program (MIPS)Hardware Architecture Description(e.g., block diagrams) CompilerAssemblerMachine Interpretationtemp = v[k];v[k] = v[k+1];v[k+1] = temp;0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111 Logic Circuit Description(Circuit Schematic Diagrams)Architecture ImplementationAnything can be representedas a number, i.e., data or instructions01/14/2019 3Spring 2011 -- Lecture #3We are here!Agenda•Scheme vs. Java vs. C•Administrivia•Quick Start Introduction to C•Technology Break•Pointers•Summary01/14/2019 Spring 2011 -- Lecture #3 4Introduction to C“the Universal Assembly Language”•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!•“Some” C experience is required before CS61CC++ or Java OK01/14/2019 5Spring 2011 -- Lecture #3Disclaimer•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 management01/14/2019 Spring 2011 -- Lecture #3 6C vs. JavaC JavaType of Language Function Oriented Object Oriented Program-ming 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 #include<stdio.h>int main(void) {printf("Hello\n");return 0;}public class HelloWorld {public static void main(String[] args) {  System.out.printl("Hello");}} StorageManual (malloc, free)Automatic (garbage collection)01/14/2019 Fall 2010 -- Lecture #1 7From http://www.cs.princeton.edu/introcs/faq/c2java.htmlBasic C ConceptsCompilerTyped variablesTyped functionsHeader files (.h)StructsEnumsPointersCreates useable programs from C sourceKind of data that a variable containsThe kind of data returned from a functionDeclare functions and variables in a separate fileGroups of related valuesLists of predefined valuesAliases to other variables01/14/2019 Spring 2011 -- Lecture #3 8These concepts distinguish C from other languages you may knowCompilation: 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 9Compilation: 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, parallelism01/14/2019 Spring 2011 -- Lecture #3 10Compilation: 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 cycle01/14/2019 Spring 2011 -- Lecture #3 11Typed Variables in Cint variable1 = 2;float variable2 = 1.618;char variable3 = 'A';•You have to declare the type of data a variable will hold–Types can't change01/14/2019 Spring 2011 -- Lecture #3 12Type Description Examplesint integer numbers, including negatives 0, 78, -1400unsigned int integer numbers (no negatives) 0, 46, 900float floating point decimal numbers 0.0, 1.618, -1.4char single text character or symbol 'a', 'D', '?’double greater precision FP numberlong larger signed integerTyped Functions in Cint numberOfPeople (){ return 3;}float dollarsAndCents (){ return 10.33;}char firstLetter (){ return 'A';}•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


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