CS 330 Lecture 18 – Types
Agenda
- what ?s
- midterm date?
- the qualities of type systems
- explicit vs. implicit
- static vs. dynamic
- strong vs. weak
- casting
- atoi
- itoa
- weak typing examples
TODO
- Browse the C FAQ. On a 1/4 sheet, briefly describe three questions that surprise, disturb, enlighten, or otherwise interest you.
Note
Let’s start with a Fill in the Blank…
Types are like…
A primary advantage of high-level languages over assembly is a type system. In short, we can define a type as a designation of the following:
- the possible values that a datum may take on
- the possible operations that may be applied to the datum
Types serve as annotations or clues to the compiler. By providing a type, you give the compiler some information that it can use to verify that you’re doing legal things with your data.
Types do not necessarily appear in the machine code, but certainly influence which instructions are generated and what sizes and offsets are used to index memory.
We’ll look at how a language’s type system is classified in the following spectra: explicit vs. implicit, static vs. dynamic, strong vs. weak.
Then we’ll write some examples in C that conform to or violate the type system. Our first exercise will be a Program This:
Implement
atoi(const char *s)
, which turns a C string into anint
. Assumes
is comprised only of digits and doesn’t exceed the capacity of anint
.
Code
atoi.c
/usr/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'coderay' (>= 0) among 56 total gem(s) (Gem::MissingSpecError) Checked in 'GEM_PATH=/.gem/ruby/2.7.0:/var/lib/gems/2.7.0:/usr/lib/ruby/gems/2.7.0:/usr/share/rubygems-integration/2.7.0:/usr/share/rubygems-integration/all:/usr/lib/x86_64-linux-gnu/rubygems-integration/2.7.0:/home/johnch/.gems', execute `gem env` for more information from /usr/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec' from /usr/lib/ruby/2.7.0/rubygems/core_ext/kernel_gem.rb:62:in `gem' from ./coderay:24:in `'