Data Types
description
Transcript of Data Types
![Page 1: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/1.jpg)
July 8th, 2009
![Page 2: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/2.jpg)
Expressions
Data Types
Arithmetic operations Logical operations
![Page 3: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/3.jpg)
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
![Page 4: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/4.jpg)
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
+ + + + 4 * b^0
3 * b^1
2 * b^2
0 * b^31 * b^4
![Page 5: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/5.jpg)
Variables are symbolic names for specific memory locations which can store data.
int x = 5;int y = 2;
x
y
2
5
RAM
![Page 6: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/6.jpg)
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
![Page 7: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/7.jpg)
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.
![Page 8: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/8.jpg)
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
![Page 9: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/9.jpg)
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;
![Page 10: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/10.jpg)
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;
![Page 11: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/11.jpg)
Variables are symbolic names for specific memory locations which can store data.
int x = 5;int y = 2;y = x;
x
y
5
5
RAM
![Page 12: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/12.jpg)
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
![Page 13: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/13.jpg)
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
![Page 14: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/14.jpg)
int x = 023; //Is in octcal (leading 0).int x = 0xA4F01; //Is in hexadecimalprintf(“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);
![Page 15: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/15.jpg)
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);
![Page 16: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/16.jpg)
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.
![Page 17: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/17.jpg)
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
![Page 18: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/18.jpg)
To use this you should include stdbool.h
_Bools have value of either 0 (true) or 1 false)
Display with %i.
![Page 19: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/19.jpg)
Program 4.1
![Page 20: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/20.jpg)
long long long
short
unsigned
signed
const
![Page 21: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/21.jpg)
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);
![Page 22: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/22.jpg)
Long long int x = 101010101000035135L;
printf(“x as a long long %lli”, x);
Guaranteed to be 64 bits
![Page 23: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/23.jpg)
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.
![Page 24: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/24.jpg)
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.
![Page 25: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/25.jpg)
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.
![Page 26: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/26.jpg)
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.
![Page 27: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/27.jpg)
- (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)
![Page 28: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/28.jpg)
Program 4.2 Program 4.3 Program 4.4
![Page 29: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/29.jpg)
In groups of 2 or 3 (or individually)Indicate the the order of execution of the
individual operators within the expression.1.X2
2.X3 – X2 +13.1 - X / Y / X *34.X - Y *(1 – (X/Y *5) +1) 5.X * Y – 2 * X/(1 + Y) * Y
![Page 30: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/30.jpg)
Operators with higher precedence are executed first.
Operators with the same precedence are executed left to right.
![Page 31: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/31.jpg)
! 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
![Page 32: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/32.jpg)
|||| TT FF
TT TT TT
FF TT FF
==== TT FF
TT TT FF
FF FF TT
TT
!! FFFF
TT
&&&& TT FF
TT FF FF
FF FF FF
![Page 33: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/33.jpg)
Let p = T, r = F, q = T and s = TEvaluate the following expressions1 !(p || r) && q2 !(p && r) || !(s == q)3 (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:
XX TT FF
TT FF TT
FF TT FF
![Page 34: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/34.jpg)
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)
![Page 35: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/35.jpg)
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
![Page 36: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/36.jpg)
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);
![Page 37: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/37.jpg)
Program 4.5
![Page 38: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/38.jpg)
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.
![Page 39: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/39.jpg)
Real work with complex numbers: √(-1)
These are optional types (not all compilers recognize these types).
![Page 40: Data Types](https://reader034.fdocuments.us/reader034/viewer/2022051620/56813b70550346895da47b49/html5/thumbnails/40.jpg)
int, double, float, char Expression &&, ||, ! , == +,-,*,/, %