Data Types - Syracuse University
Download
Report
Transcript Data Types - Syracuse University
July 8th, 2009
Expressions
Data Types
Arithmetic operations
Logical operations
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
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
+ 0 * b^3 + 2 * b^2 + 3 * b^1 + 4 * b^0
Variables are symbolic names for specific
memory locations which can store data.
int x = 5;
int y = 2;
RAM
x
2
y
5
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
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.
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
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;
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 are symbolic names for specific
memory locations which can store data.
int x = 5;
int y = 2;
y = x;
RAM
x
5
y
5
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
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
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);
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);
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.
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
To use this you should include stdbool.h
_Bools have value of either 0 (true) or 1 false)
Display with %i.
Program 4.1
long
long long
short
unsigned
signed
const
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 int x = 101010101000035135L;
printf(“x as a long long %lli”, x);
Guaranteed to be 64 bits
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 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.
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.
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.
+
++
-*
/
%
()
(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)
Program 4.2
Program 4.3
Program 4.4
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 +1
3. 1 - X / Y / X *3
4. X - Y *(1 – (X/Y *5) +1)
5. X * Y – 2 * X/(1 + Y) * Y
Operators with higher precedence are
executed first.
Operators with the same precedence are
executed left to right.
!
Not
==
Equals
!=
Not equal
&&
And
||
Or
& Bitwise AND
|
Bitwise OR
^
Bitwise XOR
!T = F, !F = T
T==T : F==F
T!=F : F != T
T && T = T
F || F = F
&&
T
F
==
T
F
T
F
F
T
T
F
F
F
F
F
F
T
||
T
F
T
T
T
F
T
F
!
T
F
F
T
Let p = T, r = F, q = T and s = T
Evaluate the following expressions
1 !(p || r) && q
2 !(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:
X
T
F
T
F
T
F
T
F
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)
1001
&0111
0 001
1001
| 0111
1111
1001
^ 0111
1110
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);
Program 4.5
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.
Real work with complex numbers: √(-1)
These are optional types (not all compilers
recognize these types).
int, double, float, char
Expression
&&, ||, ! , ==
+,-,*,/, %