400 likes | 552 Views
Data Types. July 8 th , 2009. Overview. Expressions Data Types Arithmetic operations Logical operations. Bits and Bytes. The basic building block of ram, harddrive, etc is the bit. A bit can hold either a 0 or 1. Bits are grouped into bytes: 1Byte = 8 bits 1024 bytes = 1 Kilo byte
E N D
Data Types July 8th, 2009
Overview • Expressions • Data Types • Arithmetic operations • Logical operations
Bits and Bytes • The basic building block of ram, harddrive, etc is the bit. • A bit can hold either a 0 or 1. • Bits are grouped into bytes: • 1Byte = 8 bits • 1024 bytes = 1 Kilo byte • 1024 Kilobytes = 1 Mega byte • 1024 Megabytes = 1 Giga byte
Number Base • Binary (base 2): 0101010 = 42 • Octal (base 8): 10742 = 4578 • Hexadecimal (base 16): AF01A = 44862 • Decimal (base 10) : 51,000 = 51,000 10234 in base b + 1 * b^4 + 2 * b^2 + 3 * b^1 + 4 * b^0 0 * b^3
Variables • Variables are symbolic names for specific memory locations which can store data. int x = 5; int y = 2; RAM 2 x y 5
Variable Naming Convention • Must begin with letter or _ • The rest of the name can be any combination of letters, 0-9 or _ • All variable names are case sensitive: • X and x are separate variables • Invalid variable names: • $patrick • return • 51A • my variable
Variable Naming Conventions • Variables can be at most 63 characters long, however only the first 31 characters will count. • It is often necessary to convey some meaning with variable names • variable1 – conveys very little meaning for how this variable is used. • sum - tells the programmers that this variable is used to sum up values.
Reserved Words can not be used as variable names • auto • const • double • float • int • short • struct • unsigned • break • continue • else • for • long • signed • switch • void • case • default • enum • goto • register • sizeof • typedef • volatile • char • do • extern • if • return • static • union • while
Expressions • Composed of terms and operators. • Variable declaration: int x; • Assignment expression: x = y; • Arithmetic expression: x + y; • Logical expression: x == y; • Variable declaration and assignment: • int x= 5; • Variable declaration, assignment, and arithmetic: • int x = y + z;
Assignment Expression • Variable = expression; • Where the type of value and the type of expression are equal. • Examples: • int x = 5; • int y = x; • char c = ‘c’; • char d = c;
Variables • Variables are symbolic names for specific memory locations which can store data. int x = 5; int y = 2; y = x; RAM 5 x y 5
Data Types • int : whole numbers -534,2,1200,-1110 • float: decimals: -1.2, 4, 1200.5024 • double: same as float but twice the precision • char: a single character: ‘a’, ‘1’, ‘;’, ‘\n’ • _Bool: represents true or false: 0 or 1 • void: No type
Constant Expressions • Literal numbers, character strings are constants • return 0; • 0 is a constant • printf(“Welcome to CPS 196!\n”); • “Welcome to CPS 196!\n” is a string literal
ints and displaying ints int x = 023; //Is in octcal (leading 0). int x = 0xA4F01; //Is in hexadecimal printf(“x in base 10: %d ”, x); printf(“x in base 8: %o ”, x); printf(“x in base 8 (with leading 0): %#o ”, x); printf(“x in base 16 %x ”, x); printf(“x in base 16 (with leading 0x) %#x ”, x); printf(“x in base 16 (with leading 0x and capital letters) %X ”, x);
floats and displaying floats • Fractional values are • float x = 1.5; • float y =2.0e3; //2.0 X 10^(-3) = 0.002 //2.0 is the mantissa, 3 is the exponent. • printf(“x as a float: %f”, x); • printf(“x in scientific location %e”, x); • printf(“x lets printf decide: %g”, x);
doubles and displaying doubles • Same as floats except have twice as much precision (takes up twice as much memory). • To make 1.0 a float rather than a double we use 1.0f (or 1.0F) • %f,%e,%g the same as float are used in printf.
char and displaying chars • A character is a single a character in single quotes ‘’. Can be displayed using “%c”. • ‘0’ is not the same as 0. • \ is a special symbol: • ‘\n’ - is a single character • ‘\t’ - tab • ‘\a’ – audible alert • ‘\b’ – backspace • ‘\r’– carriage return • ‘\v’ –vertical tab ‘\\’ – backslash ‘\”’ - double quote ‘\’’ – single quote ‘\?’ – question mark
_Bool • To use this you should include stdbool.h • _Bools have value of either 0 (true) or 1 false) • Display with %i.
Source Code Program 4.1
Type Specifiers (Modifiers) • long • long long • short • unsigned • signed • const
long • long int x = 50000000000L; • printf(“x as a long: %li”, x); • May extend the range of an int • Long double y = 1.234e+7L; • printf(“y as a long double %Lf”, y);
long long • Long long int x = 101010101000035135L; • printf(“x as a long long %lli”, x); • Guaranteed to be 64 bits
short • short int x = 5; • printf(“x as a short int: %hi”, x); • No way to make constant a short. • Uses less room. • Will be at least 16 bits.
unsigned • unsigned int x = 5u; //(5U) • An unsigned int can only store positive numbers. • Since we don’t worry about negative numbers we can represent twice as many positive numbers given the same number of bits.
signed • If a variable is marked as signed, the most significant bit is used to indicate a + or – and the other bits are used to indicate the value.
const • A const variable is can not be changed. Both orders are acceptable • int const x = 5; • const int x = 5; • x = x + 1; // Will throw a compiler error.
Arithmetic Expressions - (binary) Subtraction x - y 5 - 3 + (binary) Addition x + y 5 + 3 ++ (unary) Increment x++ -- (unary) Decrement x-- * (binary) Multiplication x * y 5 * 3 / (binary) Division x / y 5 / 3 % (binary) Modulus x % y 5 % 3 ( ) Parenthesis (arithmetic expression)
Arithmetic Examples • Program 4.2 • Program 4.3 • Program 4.4
Class Exercise #1 • In groups of 2 or 3 (or individually) Indicate the the order of execution of the individual operators within the expression. • X2 • X3 – X2 +1 • 1 - X / Y / X *3 • X - Y *(1 – (X/Y *5) +1) • X * Y – 2 * X/(1 + Y) * Y
Operator Execution • Operators with higher precedence are executed first. • Operators with the same precedence are executed left to right.
Logical Expression ! Not !T = F, !F = T == Equals T==T : F==F != Not equal T!=F : F != T && And T && T = T || Or F || F = F & Bitwise AND | Bitwise OR ^ Bitwise XOR
Truth Tables && T F == T F T F F T T F F F F F F T || T F T F T T T ! F T F T F
Class Exercise #2 • Let p = T, r = F, q = T and s = T Evaluate the following expressions • !(p || r) && q • !(p && r) || !(s == q) • (p || !p) && (s || !s) • Which of the above three will always be true regardless of the value of the variables involved? • Using &&, || and !, (do not use ==) create an expression of two variables whose truth table evaluates to: X T F T F T F T F
Solutions p = T, r = F, q = T and s = T !(p || r) && q = !(T || F) && T = !T && T = F &&T = F !(p && r) || !(s == q) = !(T && F) || !(T == T) = !F || !T = T||F =T (p || !p) && (s || !s) = (T || !T) && (T || !T) = (T||F)&&(T||F)= T &&T = T (3) Is a tautology (always true). (p && !q) || (!p && q) (p ||q ) && ( !p || !q) (p||q) && !(p && q)
Bitwise Operations 1 0 0 1 & 0 1 1 1 0 0 0 1 1 0 0 1 | 0 1 1 1 1 1 1 1 1 0 0 1 ^ 0 1 1 1 1 1 1 0
Type Casting We can convert from one type to another type • float x = 1.5; • int xi = (int)x; //.5 will be dropped What will the output of the below text be? const int x = 50; char c = 'c'; int cint = (int)c; char d = (char)x; printf("%i\n", cint); printf("%c\n", d);
Source Code Program 4.5
Assignment and Arithmetic • int x = 0; • x += 5; // x = x + 5; • x -= 5; // x = x – 5; • x *= 5; // x = x * 5; • x /= 5; // x = x / 5; These short hand notations are easier to read, easier to write, can execute faster.
_Complex and _Imaginary • Real work with complex numbers: √(-1) • These are optional types (not all compilers recognize these types).
int, double, float, char • Expression • &&, ||, ! , == • +,-,*,/, %