11C 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()23• Integral types:* On hats; size is system-dependentC Integral Data Types (Review)A non-negative integer4*unsigned longAn integer4*(signed) longA non-negative integer4*unsigned intAn integer4*(signed) intA small non-negative integer2*unsigned shortA small integer2*(signed) shortThe numeric code of a character1unsigned charThe numeric code of a character1signed charTypically Used to StoreBytesType4Characters35Using 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 Interchange0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 150 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US32 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 O80 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 o112 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)47char Constants•C has char constants (sort of) *•Exampleshexadecimal form01100001 '\x61'octal form01100001'\o141'digit00110000'0'NoteBinary Representation(assuming ASCII)Constantletter01100001 'a'Use single quotes for char constantUse double quotes for string constant* Technically 'a' is of type int; automatically truncated to type char when appropriate8More char Constantsnull00000000'\0'double quote00100010'\"'single quote00100111'\''question mark00111111'\?'backslash01011100'\\'vertical tab00001011'\v'horizontal tab00001001'\t'carriage return00001101 '\r'newline00001010 '\n'form feed00001100'\f' backspace00001000'\b' NoteBinary Representation(assuming ASCII)Constantalert (bell) 00000111 '\a'• Escape charactersUsedoften59Reading and Writing a Character• Subset of C I/O functions:int c;c = getchar(); /* Reads from stdin */Read a charint status;status = putchar('a'); /* Writes to stdout */Write a charExample Function CallsTask#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 -1611Using 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:•intis 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 formx = 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 arrays713Floating-Point Numbers14• Floating-point types:* On hats only; size is system-dependentC Floating-Point Data TypesA high-precision/range floating-point number12*long doubleA floating-point number8*doubleA low-precision/range floating-point number4*floatTypically Used to StoreBytesType815The 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)Closest to 0 (approx.)1.175494E-38FSmallest (approx.)-3.402823E38FLargest (approx.)3.402823E38FTypicalTypicalNote1.23456E2FConstant123.456FNote “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)Closest to 0 (approx.)2.225074E-308Smallest (approx.)-1.79693E308Largest (approx.)1.797693E308TypicalTypicalNote1.23456E2Constant123.456Decimal point or “E”indicates floating point917The 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)Closest to 0 (approx.)3.362103E-4932LSmallest (approx.)-1.189731E4932LLargest (approx.)1.189731E4932LTypicalTypicalNote1.23456E2LConstant123.456LNote “L” suffix18Data Types: C vs. Javalong double(no equivalent)(no equivalent)booleanchar comprises 1 byte(often ASCII)char comprises 2 bytes (Unicode)char is one byteSizes of all other types unspecifiedSizes of all types specifiedunsigned types(no equivalent)(no equivalent)byteCJavaRecall Java goal:Portability Æ specify sizesRecall C goal:Create an OS Æ use natural word size1019C OperatorsCombine with constants and variables to form expressionsMost C operators are familiar from Java…20Familiar C Operators• Same as Java• Refer to book for precedence and associativityexpr1?expr2:expr3Conditional(type)exprCastfunc(paramlist)Function Call!expr expr1&&expr2 expr1||expr2Logicalexpr1<expr2 expr1<=expr2 expr1>expr2 expr1>=expr2
View Full Document