Upload
claude
View
40
Download
0
Embed Size (px)
DESCRIPTION
C Data Types. Chapter 7 And other material. Representation. long (or int on linux) Two’s complement representation of value. 4 bytes used. (Where n = 32). #include limits.h. INT_MIN. INT_MAX. [ -2147483648, 2147483647]. Representation (cont.). float 4 bytes used. #include float.h - PowerPoint PPT Presentation
Citation preview
C Data Types
Chapter 7 And other material
Representation long (or int on
linux) Two’s complement
representation of value.
4 bytes used. (Where n = 32)
122 11 nton
[ -2147483648, 2147483647]
INT_MAXINT_MIN
#include limits.h
Representation (cont.) float
4 bytes used. #include float.hOn my machine, linux:
FLT_MIN=0.000000FLT_MAX=340282346638528859811704183484516925440.000000
On my laptop, Windows Xp Pro:
FLT_MIN=0.000000FLT_MAX=340282346638528860000000000000000000000.000000
Representation (cont.) double
8 bytes used. #include float.hOn my machine, linux:
DBL_MIN=2.225074e-308DBL_MAX=1.797693e+308
On my laptop, Windows Xp Pro:
DBL_MIN=2.225074e-308DBL_MAX=1.797693e+308
C Scalar Types
Simple types char int float double
Scalar, because only one value can be stored in a variable of each type.
Check Inside Your Program
Don’t depend on your assumptions for size.
Use the internal variables INT_MAX, INT_MIN to verify what you believe to be true.
Otherwise, you’ll overflow a variable.i = INT_MAX;
printf(“%d %d\n”, i, i+1); // What prints?
Check Inside Your Program
Don’t depend on your assumptions for size.
Use the internal variables INT_MAX, INT_MIN to verify what you believe to be true.
Otherwise, you’ll overflow a variable.i = INT_MAX;
printf(“%d %d\n”, i, i+1); // What prints?
2147483647 -2147483648
Numerical Inaccuracies
int sum = 0;
for(i=0; i<1000; i++) sum = sum + 1.55; printf("sum 1.55 1000 times = %f\n", sum);
What prints?
Numerical Inaccuracies
float sum = 0.0;
for(i=0; i<1000; i++) sum = sum + 1.55; printf("sum 1.55 1000 times = %f\n", sum);
What prints?
sum 1.55 1000 times = 1550.010864
???
Floating Point Must contain a
decimal point (0.0, 12.0, -0.01)
Can use scientific notation 1.1254e+12 -4.0932e-18
12101254.1 x18100932.4 x
char data type
One byte per character. Collating sequence
‘a’ < ‘b’ < ‘c’ < ‘d’ < … ‘A’ < ‘B’ < ‘C’ < ‘D’ < … ‘0’ < ‘1’ < ‘2’ < ‘3’ < … But ‘a’ < ‘A’ or ‘A’ < ‘a’ ??? Not for
sure!
User 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; <- legal int flag = 0; <- same as
User Defined Types (typedef)
Many more uses (later)
Enumerated 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 Sunday+1 and this would be meaningless.
A better way is using enumerated types.
Enumerated Types (cont.)
Example:
typedef enum
{monday, tuesday, wednesday, thursday, friday,
saturday, sunday} DayOfWeek_t
• Some default identification for user defined types
• _t
• Explicitly specify the values!
Enumerated (cont.) Now, you can define a new variable DayOfWeek_t WeekDays; WeekDays = monday; <- legal WeekDays = 12; <- illegal WeekDays = someday; <- illegal Now, internally, the computer
associates 0,1,2,… with monday, tuesday,… But you don’t have to worry!
Enumerated 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.
Enumerated (cont.)
if(today == saturday) tomorrow = sunday; else tomorrow = (DayOfWeek_t)
(today+1);
Enumerated (cont.)
for(today=monday; today <= friday; ++today) { … }
Passing 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 it’s called.
Let’s look at a simple example similar to the evaluate example in the text.
E.G. Passing a function
#include <stdio.h>#include <math.h>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));}
E.G. Passing a function
#include <stdio.h>#include <math.h>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
E.G. Passing a function
#include <stdio.h>#include <math.h>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
E.G. Passing a function
#include <stdio.h>#include <math.h>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
Lab #6 : Trapezoidal Rule
Write a program to solve for the area under a curve y = f(x) between the lines x=a and x=b. (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.)
Simple version of fig 7.13
y
x
(x0,y0)
(x1,y1)
(x2,y2) (x3,y3
)
(x4,y4)
y = f(x)
x0=a x1 x2 x3 X4
n = 4
Lab #6 : assumptions Function is
positive over the interval [a,b].
(for n subintervals of length h) h=(b-a)/n Trapezoidal rule is: ))(2)()((
2
1
1
n
iixfbfaf
hT
Lab #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
)14159.3,0(
)sin()( 2
bafor
xxxg
)2,2(
4)( 2
bafor
xxh
Lab #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
b
a
dxxf )(
Exam #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.