CSC195, Class 13: Fundamental Types in C Overview * What is a type? * Integer types * Floating-point types * Other key types * Enumerated types * Key numeric operations * Lab Notes * Take-home exam next week ---------------------------------------- What is a type? * A mechanism for converting sequences of 0's and 1's to other kinds of values. + As such, it speaks to the amount of space a value takes. * A type is a set of values * A type is a set of restrictions on when a value can be used in an expression + Conversely, it's a set of capabilities that suggest when the value can be used. + When can I call (sqrt x) + Why we usually type values * Inverse of anti-type * A type is a distinguishable type of object * How do you define a set without enumerating the values? Recursively and with other set operations Languages typically provide different kinds of types * Primitive types (directly correspond to bit patterns) * Mechanisms for building compound types (e.g., records, classes) from types * Mechanisms for naming types Primitive Types in C: Primarily Numeric * Integers int unsigned int short int (also just "short") unsigned short int long int (also just "long") unsigned long int long long int * Very few specifications on size * Floating Point Numbers float double long float * No specification on size (in spite of names) * No specification on encoding Other basic types are essentially integers * char: just integers (typically one byte) * boolean: just integers (typically int size) Simplest user-defined types: enumerated types enum day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }; Why bother? * Programs can be easier to read and understand. "int x" vs "enum day x" "x = 2" vs "x = WEDNESDAY" Operations: All the ones you expect. ++b vs. b++ for (i = 0; i < 10; ++i) vs. for (i = 0; i < 10; i++) Only matters if in an assignmetn, procedure call, or argument to an operator C++ --- x += exp EValuate exp, increment x by that value x *= =+ x=+2 <- Ambiguous a = b++ - b++ ---------------------------------------- * The smallest unsigned integer of any kinds is 0 (ZERO) * You'll need to choose the right parameter for printf, E.g., printf("%u\n", UINT_MAX); * Write a little, test a little, write a little, test a little.