C Data Types - PowerPoint PPT Presentation

About This Presentation
Title:

C Data Types

Description:

double. Scalar, because only one value can be stored in a variable of each type. ... a' b' c' d' ... A' B' C' D' ... 0' 1' 2' 3' ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 31
Provided by: csMon
Category:
Tags: data | double | types

less

Transcript and Presenter's Notes

Title: C Data Types


1
C Data Types
  • Chapter 7
  • And other material

2
Representation
  • 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
3
Representation (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
4
Representation (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
5
C Scalar Types
  • Simple types
  • char
  • int
  • float
  • double
  • Scalar, because only one value can be stored in a
    variable of each type.

6
Check 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?
7
Check 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
8
Numerical Inaccuracies
What prints?
int sum 0 for(i0 ilt1000 i) sum sum
1.55 printf("sum 1.55 1000 times f\n",
sum)
9
Numerical 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
???
10
Floating Point
  • Must contain a decimal point (0.0, 12.0, -0.01)
  • Can use scientific notation
  • 1.1254e12
  • -4.0932e-18

11
char 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!

12
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 lt- legal
  • int flag 0 lt- same as

13
User Defined Types (typedef)
  • Many more uses (later)

14
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 Sunday1 and this
    would be meaningless.
  • A better way is using enumerated types.

15
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!

16
Enumerated (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!

17
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.

18
Enumerated (cont.)
  • if(today saturday)
  • tomorrow sunday
  • else
  • tomorrow (DayOfWeek_t)(today1)

19
Enumerated (cont.)
  • for(todaymonday
  • today lt friday
  • today)

20
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
    its called.
  • Lets look at a simple example similar to the
    evaluate example in the text.

21
E.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))
22
E.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
23
E.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
24
E.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
25
Lab 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.)

26
Simple 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
27
Lab 6 assumptions
  • Function is positive over the interval a,b.
  • (for n subintervals of length h) h(b-a)/n
    Trapezoidal rule is

28
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

29
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

30
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.
Write a Comment
User Comments (0)
About PowerShow.com