CS 206 C2 Lexical Elements, Operators and the C System s1
Total Page:16
File Type:pdf, Size:1020Kb
CS 206 – C3 – Fundamental Data Types
6.1 Storage, Compilers, Machine Instructions, Variables, Constants, etc.
6.2 The Fundamental Data Types
6.3 Characters and the Data Type char
Positional Notation, Bits, Bit Strings, Bytes, Collating Sequence, ASCII, EBCDIC, etc.
See p 609, App E.
6.4 The Data Type int 2’s Complement, +/- integers, unsigned integers, Half Words, Words, etc.
Hex/Octal Constants – 0377 (O) or 0x1A (H). Used to set bit strings…
6.5 The Integral Types short, long and unsigned short => typically 2 bytes (half word) (machine/compiler dependent) long => typically 4 bytes (word) (machine/compiler dependent) unsigned => typically 4 bytes (word) (machine/compiler dependent)
Arithmetic on unsigned ints is logical.
6.6 The Floating Types float vs int… float => sign, exponent, significant digits, typically 4 bytes double => typically 8 bytes (machine/compiler dependent) long double => typically 8 bytes (machine/compiler dependent)
Range vs Accuracy…
6.7 sizeof Operator
Guaranteed configurations…
Note: sizeof is an operator… if sizeof is applied to a type, parentheses are required. If not, parentheses are optional, i.e., sizeof(a + b + 7.7) .eqv. sizeof a + b + 7.7
Code to determine data type sizes…
6.8 Mathematical Functions
Mathematics library contains sqrt(), pow(), exp(), log(), sin(), cos(), tan(), etc.
Arguments are type double, function returns are type double.
Trig function arguments are in radians.
Example Code… 6.9 Conversions and Casts
Integral Promotion
Def: If all data types in an expression can be represented by an int, the expression value is converted to an int. Otherwise the expression value is converted to an unsigned int. Example, char c = ‘A’; printf(“%c\n”, c); Type of the expression c in printf above, is int, not char. Automatic Conversion Examples
Casts (Type Converters)
(double) i Expression type is double (i type not changed) (long) (‘A’ + 1.0) Expression type is long x = (float) ((int) y + 1) Expression types are int, float (double) (x = 77) Expression type is double (x type not changed)
Cast Operator has higher precedence than arithmetic operators, i.e., (float) i + 3 .eqv. ((float) i) + 3
6.13 System Considerations
. IEEE Standard for Floating Point Arithmetic . Cray Integers . Turbo C – “A long double is stored in ten bytes.” => Doubtful!!!