This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: The semantics of C Readings: Readings from CP, chapters 110, as detailed in the document available on the Web site. • The memory model and C • Tail recursion and loops • Stacks for function calls in C and Scheme CS 136 Winter 2009 05: The semantics of C 1 Types in C So far: int , represented using a single 32bit word. C integers are not unbounded as in Scheme. The legal range is 2 , 147 , 483 , 648 to 2 , 147 , 483 , 647 (exactly 2 32 possibilities). Words with 0 as the most significant bit are interpreted as natural numbers in binary notation. Words with 1 as the most significant bit are interpreted as negative numbers. The word with all bits 1 represents 1 . CS 136 Winter 2009 05: The semantics of C 2 Numerical operations such as addition or multiplication which result in integers outside the legal range do not automatically cause a program error. Some incorrect value may be used instead. GCC typically generates code where only the lower 32 bits of a larger result are preserved. The addition of two positive numbers can (if overflow occurs) produce a negative result. CS 136 Winter 2009 05: The semantics of C 3 float (floatingpoint): intended to approximate real numbers. A float value uses the same 32bit representation (“IEEE 754”) as an inexact number in Scheme. These really are approximations. Even a simple number like 1 . 2 cannot be represented exactly. Effective computation using floatingpoint numbers is a deep and complex topic. CS 136 Winter 2009 05: The semantics of C 4 char : the character type, using only 8 bits. Characters are basically treated as small integers in C. The C function getchar() (the equivalent of readchar ) produces an int , not a char . Because all 256 8bit patterns are possible characters, getchar needs another value to represent endoffile (it uses the constant EOF ). ’a’ is a C character constant. As in Scheme, backslashes are used for “escape sequences”. The newline character is ’\n’ . We will discuss C strings (a compound type) in lecture module 07. CS 136 Winter 2009 05: The semantics of C 5 Dynamic and static typing In Scheme, types are associated with values, not variables. A function f with a parameter x could be applied in the expressions ( f 3 ) and ( f ’ cat ) , as long as f can deal with it. With ( define ( f x ) ( + x 4 )) , evaluating ( myfn ’ cat ) will cause a runtime error. This is known as dynamic typing . CS 136 Winter 2009 05: The semantics of C 6 In contrast, C uses static typing . Every variable and every expression must have a type that can be determined at compile time. • This can catch some bugs at compile time instead of run time • The compiler can generate faster code • Type information can be discarded after compilation, saving space A C program containing the declaration int i will result in one 32bit word of memory being used for the value of i ....
View
Full
Document
This note was uploaded on 08/31/2011 for the course CS 136 taught by Professor Becker during the Fall '08 term at Waterloo.
 Fall '08
 BECKER
 Recursion

Click to edit the document details