05-semantics-post3up

05-semantics-post3up - The semantics of C Readings:...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: The semantics of C Readings: Readings from CP, chapters 1-10, 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 32-bit 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 (floating-point): intended to approximate real numbers. A float value uses the same 32-bit 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 floating-point 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 read-char ) produces an int , not a char . Because all 256 8-bit patterns are possible characters, getchar needs another value to represent end-of-file (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 ( my-fn cat ) will cause a run-time 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 32-bit word of memory being used for the value of i ....
View Full Document

Page1 / 23

05-semantics-post3up - The semantics of C Readings:...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online