Title: C Data Types
1C Data Types
- Chapter 7
- And other material
2Representation
- long (or int on linux)
- Twos complement representation of value.
- 4 bytes used.
- (Where n 32)
-
include limits.h
INT_MAX
INT_MIN
-2147483648, 2147483647
3Representation (cont.)
- float
- 4 bytes used.
- include float.h
- On my machine, linux
FLT_MIN0.000000 FLT_MAX3402823466385288598117041
83484516925440.000000
On my laptop, Windows Xp Pro
FLT_MIN0.000000 FLT_MAX3402823466385288600000000
00000000000000.000000
4Representation (cont.)
- double
- 8 bytes used.
- include float.h
- On my machine, linux
DBL_MIN2.225074e-308 DBL_MAX1.797693e308
On my laptop, Windows Xp Pro
DBL_MIN2.225074e-308 DBL_MAX1.797693e308
5C Scalar Types
- Simple types
- char
- int
- float
- double
- Scalar, because only one value can be stored in a
variable of each type.
6Check Inside Your Program
- Dont depend on your assumptions for size.
- Use the internal variables INT_MAX, INT_MIN to
verify what you believe to be true. - Otherwise, youll overflow a variable.
i INT_MAX printf(d d\n, i, i1) //
What prints?
7Check Inside Your Program
- Dont depend on your assumptions for size.
- Use the internal variables INT_MAX, INT_MIN to
verify what you believe to be true. - Otherwise, youll overflow a variable.
i INT_MAX printf(d d\n, i, i1) //
What prints?
2147483647 -2147483648
8Numerical Inaccuracies
What prints?
int sum 0 for(i0 ilt1000 i) sum sum
1.55 printf("sum 1.55 1000 times f\n",
sum)
9Numerical Inaccuracies
What prints?
float sum 0.0 for(i0 ilt1000 i) sum
sum 1.55 printf("sum 1.55 1000 times
f\n", sum)
sum 1.55 1000 times 1550.010864
???
10Floating Point
- Must contain a decimal point (0.0, 12.0, -0.01)
- Can use scientific notation
- 1.1254e12
- -4.0932e-18
11char data type
- One byte per character.
- Collating sequence
- a lt b lt c lt d lt
- A lt B lt C lt D lt
- 0 lt 1 lt 2 lt 3 lt
- But a lt A or A lt a ??? Not for sure!
12User Defined Types (typedef)
- This is how you can expand the types available to
a particular program. - typedef type-declaration
- E.g. typedef int count
- Defines a new type named count that is the same
as int. - count flag 0 lt- legal
- int flag 0 lt- same as
13User Defined Types (typedef)
14Enumerated Types
- In the old days, we would make an assignment like
1 means Monday, 2 means Tuesday, 3 means
Wednesday - But this way, you could have Sunday1 and this
would be meaningless. - A better way is using enumerated types.
15Enumerated Types (cont.)
typedef enum monday, tuesday, wednesday,
thursday, friday, saturday, sunday
DayOfWeek_t
- Some default identification for user defined
types - _t
- Explicitly specify the values!
16Enumerated (cont.)
- Now, you can define a new variable
- DayOfWeek_t WeekDays
- WeekDays monday lt- legal
- WeekDays 12 lt- illegal
- WeekDays someday lt- illegal
- Now, internally, the computer associates 0,1,2,
with monday, tuesday, But you dont have to
worry!
17Enumerated rules
- Enumerated constants must be identifiers, NOT
numeric (1,3,-4), character (s, t, p), or
string (This is a string) literals. - An identifier cannot appear in more than one
enumerated type definition. - Relational, assignment, and even arithmetic
operators can be used.
18Enumerated (cont.)
- if(today saturday)
- tomorrow sunday
- else
- tomorrow (DayOfWeek_t)(today1)
19Enumerated (cont.)
- for(todaymonday
- today lt friday
- today)
-
20Passing a Function Name as a Parameter
- In C it is possible to pass a function name as a
parameter. - Gives the called function the ability to do
something using different functions each time
its called. - Lets look at a simple example similar to the
evaluate example in the text.
21E.G. Passing a function
include ltstdio.hgt include ltmath.hgt double
evaluate(double f( ), double) int main (void)
double sqrtvalue, sinvalue
sqrtvalue evaluate(sqrt, 12.5)
printf("f \n", sqrtvalue) sinvalue
evaluate(sin, 0.5) printf("f \n",
sinvalue) double evaluate ( double f(double
f_arg), double pt1) return
(f(pt1))
22E.G. Passing a function
include ltstdio.hgt include ltmath.hgt double
evaluate(double f( ), double) int main (void)
double sqrtvalue, sinvalue
sqrtvalue evaluate(sqrt, 12.5)
printf("f \n", sqrtvalue) sinvalue
evaluate(sin, 0.5) printf("f \n",
sinvalue) double evaluate ( double f(double
f_arg), double pt1) return
(f(pt1))
3.535534 0.479426
23E.G. Passing a function
include ltstdio.hgt include ltmath.hgt double
evaluate(double f( ), double) int main (void)
double sqrtvalue, sinvalue
sqrtvalue evaluate(sqrt, 12.5)
printf("f \n", sqrtvalue) sinvalue
evaluate(sin, 0.5) printf("f \n",
sinvalue) double evaluate ( double f(double
f_arg), double pt1) return
(f(pt1))
3.535534 0.479426
24E.G. Passing a function
include ltstdio.hgt include ltmath.hgt double
evaluate(double f( ), double) int main (void)
double sqrtvalue, sinvalue
sqrtvalue evaluate(sqrt, 12.5)
printf("f \n", sqrtvalue) sinvalue
evaluate(sin, 0.5) printf("f \n",
sinvalue) double evaluate ( double f(double
f_arg), double pt1) return
(f(pt1))
3.535534 0.479426
25Lab 6 Trapezoidal Rule
- Write a program to solve for the area under a
curve y f(x) between the lines xa and xb.
(See figure 7.13 on page 364. - Approximate this area by summing trapezoids
(Formed by a line from x0 vertical up to the
function, to f(x0), then straight line to f(x1),
back down to the x-axis, and left to original.)
26Simple version of fig 7.13
y
(x1,y1)
(x2,y2)
y f(x)
(x3,y3)
(x0,y0)
(x4,y4)
x
x0a
x1
x2
x3
X4 n 4
27Lab 6 assumptions
- Function is positive over the interval a,b.
- (for n subintervals of length h) h(b-a)/n
Trapezoidal rule is
28Lab 6 (cont.)
- Write a function trap with input parameters a,b,n
and f that implements the trapezoidal rule. - Call trap with values for n of 2,4,8,16,32,64,
and 128 on functions
29Lab 6 (cont.)
- Function h defines a half-circle of radius 2.
Compare your approximation to the actual area of
this half-circle. - Note the trapezoidal rule approximates
30Exam 1 On Wednesday
- Closed Book!
- One 8-1/2x11 paper, both sides allowed.
- Sit with a space on either side of you.
- Only 4 function calculators allowed.
- Chapters 1-6.
- Linux.
- Makefiles.
- Introduction to Pointers.