DOC PREVIEW
Princeton COS 217 - Lecture

This preview shows page 1-2-20-21 out of 21 pages.

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

Unformatted text preview:

1 1 The Design of C: !A Rational Reconstruction (contd.)"2 Goals of this Lecture""• Recall from last lecture… "• Help you learn about:"• The decisions that were available to the designers of C"• The decisions that were made by the designers of C"… and thereby…"• C !"• Why?"• Learning the design rationale of the C language provides a richer understanding of C itself"• … and might be more interesting than simply learning the language itself !!!"• A power programmer knows both the programming language and its design rationale"2 3 Character Data Types"• Issue: What character data types should C have?"• Thought process"• The most common character codes are (were!) ASCII and EBCDIC"• ASCII is 7-bit"• EBCDIC is 8-bit"• Decisions"• Provide type char • Type char should be one byte"Was that a good decision?"4 Character Data Types (cont.)"• Tangential Decision"• char should be an integer type"• Can use type char to store small integers"• Can do arithmetic with data of type char • Can freely mix char and integer data"• ('a' + 1) is 'b' (assuming ASCII)"• ('0' + 5) is '5' (assuming ASCII)"Was that a good decision?"How does Java handle these expressions?"3 5 Character Constants"• Issue: How should C represent character constants?"• Thought process"• Could represent character constants as int constants, with truncation of high-order bytes"• More readable to use single quote syntax ('a', 'b', etc.); but then…"• Need special way to represent the single quote character"• Need special ways to represent non-printable characters (e.g. newline, tab, space, etc.)"• Decisions"• Provide single quote syntax"• Use backslash to express special characters"6 Character Constants (cont.)"• Examples"• 'a' "the a character"• (char)97" "the a character"• (char)0141 the a character"• '\o141' " "the a character, octal character form"• '\x61' " "the a character, hexadecimal character form"• '\0' " "the null character"• '\a' "bell"• '\b' "backspace"• '\f' " "formfeed"• '\n' "newline"• '\r' "carriage return"• '\t' "horizontal tab"• '\v' " "vertical tab"• '\\' " "backslash"• '\'' "single quote"4 7 Strings"• Issue: How should C represent strings?"• Thought process"• String can be represented as a sequence of chars"• How to know where char sequence ends?"• Store length before char sequence?"• Store special “sentinel” char after char sequence?"• Strings are common in systems programming"• C should be small/simple"Advantages/disadvantages?"8 Strings (cont.)"• Decisions"• Adopt a convention"• String consists of a sequence of chars terminated with the null ('\0') character"• Use double-quote syntax (e.g. "abc", "hello") to represent a string constant"• Provide no other language features for handling strings"• Delegate string handling to standard library functions"• Examples"• "abc" is a string constant"• 'a' is a char constant"• "a" is a string constant"How many"bytes?"5 9 Logical Data Type"• Issue: How should C represent logical data?"• Thought process"• Representing a logical value (TRUE or FALSE) requires only one bit"• Smallest entity that can be addressed is one byte"• Type char is one byte, so could be used to represent logical values"• C should be small/simple"10 Logical Data Type (cont.)"• Decisions"• Don't define a logical data type"• Represent logical data using type char, or any integer type"• Convention: 0 => FALSE, non-0 => TRUE"• Convention used by:"• Relational operators (<, >, etc.)"• Logical operators (!, &&, ||)"• Statements (if, while, etc.)"Was that a good decision? (See the next 2 slides)"6 11 Logical Data Type (cont.)"• Note"• Using integer data to represent logical data permits shortcuts"… int i; … if (i) /* same as (i != 0) */ statement1; else statement2; … Are such shortcuts beneficial?"12 Logical Data Type (cont.)"• Note"• The lack of logical data type cripples compiler's ability to detect some errors"… int i; … i = 0; … if (i = 5) statement1; else statement2; … How does Java handle this code?"What is the problem with this code?"What is the effect of this code?"7 13 Floating-Point Data Types"• Issue: What floating-point data types should C have?"• Thought process"• Systems programs use floating-point data infrequently"• But some application domains (e.g. scientific) use floating-point data often"• Decisions"• Provide three floating-point data types: float, double, and long double • bytes in float <= bytes in double <= bytes in long double • Incidentally, on hats using gcc217"• float: " "4 bytes"• double: " "8 bytes"• long double: "12 bytes"14 Floating-Point Constants"• Issue: How should C represent floating-point constants?"• Thought process"• Convenient to allow both fixed-point and scientific notation"• Decimal is sufficient; no need for octal or hexadecimal"• Decisions"• Any constant that contains decimal point or "E" is floating-point"• The default floating-point type is double • Append "F" to indicate float • Append "L" to indicate long double • Examples"• double: 123.456, 1E-2, -1.23456E4 • float: 123.456F, 1E-2F, -1.23456E4F • long double: 123.456L, 1E-2L, -1.23456E4L Why?"8 15 Feature 2: Operators"• A high-level programming language should have operators"• Operators combine with constants and variables to form expressions"16 Kinds of Operators"• Issue: What kinds of operators should C have?"• Thought process"• Should handle typical operations"• Should handle bit-level programming ("bit fiddling")"• Decisions"• Provide typical arithmetic operators: + - * / %"• Provide typical relational operators: == != < <= > >="• Each evaluates to 0=>FALSE or 1=>TRUE"• Provide typical logical operators: ! && ||"• Each interprets 0=>FALSE, non-0=>TRUE"• Each evaluates to 0=>FALSE or 1=>TRUE"• Provide bitwise operators: ~ & | ^ >> <<"• Provide a cast operator: (type)9 17 Assignment Operator"• Issue: What about


View Full Document

Princeton COS 217 - Lecture

Documents in this Course
Summary

Summary

4 pages

Lecture

Lecture

4 pages

Generics

Generics

14 pages

Generics

Generics

16 pages

Lecture

Lecture

20 pages

Debugging

Debugging

35 pages

Types

Types

7 pages

Assembler

Assembler

16 pages

Lecture

Lecture

20 pages

Lecture

Lecture

39 pages

Testing

Testing

44 pages

Pipeline

Pipeline

19 pages

Lecture

Lecture

6 pages

Signals

Signals

67 pages

Building

Building

17 pages

Lecture

Lecture

7 pages

Modules

Modules

12 pages

Generics

Generics

16 pages

Testing

Testing

22 pages

Signals

Signals

34 pages

Lecture

Lecture

19 pages

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