DOC PREVIEW
Penn CIS 240 - Introduction to Programming in C

This preview shows page 1-2 out of 7 pages.

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

Unformatted text preview:

1Based on slides © McGraw-HillAdditional material © 2004/2005 Lewis/MartinChapter 11Introduction toProgramming in C2CSE 240 There are 10 kinds of people in the world……those that know binary,and those that don’t.3CSE 240Aside: What is Unix? The most influential operating system First developed in 1969 at AT&T Bell Labs• By Ken Thompson and Dennis Ritchie• Designed for “smaller” computers of the day• Reject some of the complexity of MIT’s Multics They found writing in assembly tedious• Result: Dennis Ritchie invented the C programming language Introduced to UC-Berkeley (Cal) in 1974• Bill Joy was an early Unix hacker as a PhD student at Cal• Much of the early internet consisted of Unix systems Mid-80s• Good, solid TCP/IP for BSD in 1984 Linux• Free implementation of Unix (libre and gratuit)• Announced by Linus Torvalds in 1991 Much more in CSE380!4CSE 240Aside: The Unix Command Line Text-based approach to give commands• Commonly used before graphical displays• Many advantages even today Examples• mkdir cse240hw8 make a directory• cd cse240hw8 change to the directory• ls list contents of directory• cp /mnt/eniac/home1/c/cse240/project/hw/hw8/* . Copy files from one location to current dir (“.”)• emacs foo.c & run the command “emacs” with input “foo.c”• gcc -o foo foo.c compile foo.c (create program called “foo”) Unix eventually developed graphical UIs (GUIs)• X-windows (long before Microsoft Windows)25CSE 240Programming Levels Application Languages (Java, C#) System Programming Languages (C and C++) Scripting Languages (Perl, Python, VB) Assembly Language (x86, PowerPC, SPARC, MIPS) Machine Language (x86, PowerPC, SPARC, MIPS) Hardware (Application-Specific IntegratedCircuits or ASICs) High-LevelLanguages Low-LevelLanguages Compilation Assembly Interpreted Or Compiled6CSE 240The Course Thus Far… We did digital logic• Bits are bits• Ultimately, to understand a simple processor We did assembly language programming• Programming the “raw metal” of the computer• Ultimately, to understand C programming Starting today: we’re doing C programming• C is still common for systems programming• You’ll need it for the operating systems class (CSE380)• Ultimately, for a deeper understanding of any language (Java)7CSE 240Why High-Level Languages? Easier than assembly. Why?• Less primitive constructs• Variables• Type checking Portability• Write program once, run it on the LC-3 or Intel’s x86 Disadvantages• Slower and larger programs (in most cases)• Can’t manipulate low-level hardwareAll operating systems have some assembly in them Verdict: assembly coding is rare today8CSE 240Our Challenge 99% of you already know either Java or C• We’re going to try to cover the basics quickly• We’ll spend more time on pointers & other C-specific nastiness Created two decades apart• C: 1970s - AT&T Bell Labs• C++: 1980s - AT&T Bell Labs• Java: 1990s - Sun Microsystems Java and C/C++• Syntactically similar (Java uses C syntax)• C lacks many of Java’s features• Subtly different semantics39CSE 240C is Similar To Java Without: Objects• No classes, objects, methods, or inheritance Exceptions• Check all error codes explicitly Standard class library• C has only a small standard library Garbage collection• C requires explicit memory allocate and free Safety• Java has strong type checking, checks array bounds• In C, anything goes Portability• Source: C code is less portable (but better than assembly)• Binary: C compiles to specific machine code10CSE 240More C vs Java differences C has a “preprocessor”• A separate pre-pass over the code• Performs replacements Include vs Import• Java has import java.io.*;• C has: #include <stdio.h>• #include is part of the preprocessor Boolean type• Java has an explicit boolean type• C just uses an “int” as zero or non-zero• C’s lack of boolean causes all sorts of trouble More differences as we go along…11CSE 240What is C++? C++ is an extension of C• Backward compatible (good and bad)• That is, all C programs are legal C++ programs C++ adds many features to C• Classes, objects, inheritance• Templates for polymorphism• A large, cumbersome class library (using templates)• Exceptions (not actually implemented for a long time)• More safety (though still unsafe)• Operator and function overloading Thus, many people uses it (to some extent)• However, we’re focusing on only C, not C++12CSE 240Quotes on C/C++ vs Java “C is to assembly language as Java is to C”• Unknown "With all due respect, saying Java is just a C++ subset israther like saying that `Pride and Prejudice' is just asubset of the Encyclopedia Britanica. While it is true thatone is shorter than the other, and that both have thesame syntax, there are rather overwhelming differences.”• Sam Weber, on the ACM SIGSCE mailing list “Java is C++ done right.”• Unknown413CSE 240More quotes on C/C++ "The C programming language combines the power ofassembly language with the ease-of-use of assemblylanguage.”• Unknown "It is my impression that it's possible to write goodprograms in C++, but nobody does.”• John Levine, moderator of comp.compilers “C makes it easy to shoot yourself in the foot; C++ makesit harder, but when you do it, it blows your whole leg off.”• Bjarne Stroustrup, creator of C++14CSE 240Compilation vs. Interpretation Different ways of translating high-level languages Interpretation• Interpreter: program that executes program statementsDirectly interprets program (portable but slow)Limited optimization• Easy to debug, make changes, view intermediate results• Languages: BASIC, LISP, Perl, Python, Matlab Compilation• Compiler: translates statements into machine languageCreates executable program (non-portable, but fast)Performs optimization over multiple statements• Harder to debug, change requires recompilation• Languages: C, C++, Fortran, Pascal Hybrid• Java, has features of both interpreted and compiled languages15CSE 240Compilation vs. Interpretation Consider the following algorithm:• Get W from the keyboard.• X = W + W• Y = X + X• Z = Y + Y• Print Z to screen. If interpreting, how many arithmetic operations occur? If compiling, we can analyze the entire program andpossibly reduce the number of operations.• Can we simplify the


View Full Document

Penn CIS 240 - Introduction to Programming in C

Download Introduction to Programming in C
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 Programming in C 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 Programming in C 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?