DOC PREVIEW
Princeton COS 217 - C Fundamentals

This preview shows page 1-2-16-17-18-33-34 out of 34 pages.

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

Unformatted text preview:

C FundamentalsGoals of this LectureC Integral Data Types (Review)CharactersUsing char for CharactersThe ASCII Codechar ConstantsMore char ConstantsReading and Writing a CharacterThe “End-of-File Character”Using EOFStringsFloating-Point NumbersC Floating-Point Data TypesThe float Data TypeThe double Data TypeThe long double Data TypeData Types: C vs. JavaC OperatorsFamiliar C OperatorsThe sizeof OperatorDetermining Data SizesThe Sequence OperatorAdditional OperatorsOperators: C vs. JavaOperators: C vs. Java (cont.)C StatememtsC StatementsC Statements (cont.)Statements: C vs. JavaStatements: C vs. Java (cont.)Common IdiomsI/O FunctionsSummary1C FundamentalsProfessor Jennifer Rexfordhttp://www.cs.princeton.edu/~jrex2Goals of this Lecture•C data types•Integers (from last time)•Char (in more detail)•Floating point: float, double, and long double•Operators•Arithmetic, assignment, relational, logical, conditional, …•sizeof()•Statements•Expressions, declarations, if/else, switch, …•While, do-while, for, return, break, continue, goto, …•I/O functions•getchar(), putchar(), printf(), and scanf()3•Integral types: * On hats; size is system-dependentC Integral Data Types (Review)Type Bytes Typically Used to Storesigned char 1 The numeric code of a characterunsigned char 1 The numeric code of a character(signed) short 2* A small integerunsigned short 2* A small non-negative integer(signed) int 4* An integerunsigned int 4* A non-negative integer(signed) long 4* An integerunsigned long 4* A non-negative integer4Characters5Using char for Characters•Type char can be used for (limited range) arithmetic, but…•Usually used to store characters – thus the name!•Must use a code to map 1-byte numbers to characters•Common code: ASCII•Other ways to represent characters•Less common: EBCDIC•What about Unicode? “wide” characters (2 bytes)6The ASCII CodeAmerican Standard Code for Information Interchange 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI 16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 32 SP ! " # $ % & ' ( ) * + , - . / 48 0 1 2 3 4 5 6 7 8 9 : ; < = > ? 64 @ A B C D E F G H I J K L M N O 80 P Q R S T U V W X Y Z [ \ ] ^ _ 96 ` a b c d e f g h i j k l m n o 112 p q r s t u v w x y z { | } ~ DEL Lower case: 97-122 and upper case: 65-90E.g., ‘a’ is 97 and ‘A’ is 65 (i.e., 32 apart)7char Constants•C has char constants (sort of) *•ExamplesConstant Binary Representation(assuming ASCII)Note'a' 01100001 letter'0' 00110000 digit'\o141' 01100001 octal form'\x61' 01100001 hexadecimal formUse single quotes for char constantUse double quotes for string constant* Technically 'a' is of type int; automatically truncated to type char when appropriate8More char ConstantsConstant Binary Representation(assuming ASCII)Note'\a' 00000111 alert (bell) '\b' 00001000 backspace'\f' 00001100 form feed'\n' 00001010 newline'\r' 00001101 carriage return'\t' 00001001 horizontal tab'\v' 00001011 vertical tab'\\' 01011100 backslash'\?' 00111111 question mark'\'' 00100111 single quote'\"' 00100010 double quote'\0' 00000000 null• Escape charactersUsedoften9Reading and Writing a Character•Subset of C I/O functions:Task Example Function CallsWrite a char int status; status = putchar('a'); /* Writes to stdout */Read a char int c; c = getchar(); /* Reads from stdin */#include <stdio.h>int main(void) { int c; c = getchar(); if (c != EOF) { if ((c >= 'a') && (c <= 'z')) c += 'A' - 'a'; putchar(c); }}‘a’ is 97‘A’ is 6510The “End-of-File Character”•Files do not end with the “EOF character”•Because there is no such thing!!!•EOF is:•A special non-character value returned by getchar() and related functions to indicate failure•#defined in stdio.h; typically as -111Using EOF•Correct code•Equivalent idiom•Incorrect codeint c;c = getchar();while (c != EOF) { … c = getchar();}int c;while ((c = getchar()) != EOF) { …}getchar() returns int because:•int is the computer’s natural word size•getchar() must be able to return all valid chars and EOFchar c;while ((c = getchar()) != EOF) { …}What if stdin contains the 11111111 (ӱ) character?An expression of the form x = yassigns to x, and evaluatesto the new value of x12Strings•Java has a String class•String s; // OK in Java•C does not have a String data type•String s; /* Not OK in C */•Java and C have string constants•E.g. "hello"• In C, a string is a null-terminated array of characters•'a' is a char (01100001)•"a" is a string (01100001 00000000)•More later, after discussing pointers and arrays13Floating-Point Numbers14•Floating-point types: * On hats only; size is system-dependentC Floating-Point Data TypesType Bytes Typically Used to Storefloat 4* A low-precision/range floating-point numberdouble 8* A floating-point numberlong double 12* A high-precision/range floating-point number15The float Data Type•Description: •A (positive or negative) floating point number•Size: system dependent•bits in float <= bits in double <= bits in long double•Often 4 bytes; limited precision and range; infrequently used•Example constants (assuming 4 bytes)Constant Note123.456F Typical1.23456E2F Typical3.402823E38F Largest (approx.)-3.402823E38F Smallest (approx.)1.175494E-38F Closest to 0 (approx.)Note “F” suffix16The double Data Type•Description: •A (positive or negative) double-precision floating point number•Size: system dependent•bits in float <= bits in double <= bits in long double•Often 8 bytes•Example constants (assuming 8 bytes)Constant Note123.456 Typical1.23456E2 Typical1.797693E308 Largest (approx.)-1.79693E308 Smallest (approx.)2.225074E-308 Closest to 0 (approx.)Decimal point or “E” indicates floating point17The long double Data Type•Description: •A (positive or negative) floating point number•Size: system dependent•bits in float <= bits in double <= bits in long double•Often 10 or 12 bytes•Example constants (assuming 12 bytes)Constant Note123.456L Typical1.23456E2L Typical1.189731E4932L Largest (approx.)-1.189731E4932L Smallest (approx.)3.362103E-4932L Closest to 0 (approx.)Note “L”


View Full Document

Princeton COS 217 - C Fundamentals

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

Lecture

Lecture

21 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 C Fundamentals
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 C Fundamentals 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 C Fundamentals 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?