1 / 30

C Data Types

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

nancyscott
Download Presentation

C Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C Data Types Chapter 7 And other material

  2. 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]

  3. Representation (cont.) • float • 4 bytes used. • #include float.h On my machine, linux: FLT_MIN=0.000000 FLT_MAX=340282346638528859811704183484516925440.000000 On my laptop, Windows Xp Pro: FLT_MIN=0.000000 FLT_MAX=340282346638528860000000000000000000000.000000

  4. Representation (cont.) • double • 8 bytes used. • #include float.h On my machine, linux: DBL_MIN=2.225074e-308 DBL_MAX=1.797693e+308 On my laptop, Windows Xp Pro: DBL_MIN=2.225074e-308 DBL_MAX=1.797693e+308

  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 • 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?

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

  8. Numerical Inaccuracies What prints? int sum = 0; for(i=0; i<1000; i++) sum = sum + 1.55; printf("sum 1.55 1000 times = %f\n", sum);

  9. Numerical Inaccuracies What prints? float sum = 0.0; for(i=0; i<1000; 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.1254e+12 • -4.0932e-18

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

  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; <- legal • int flag = 0; <- 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 Sunday+1 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; <- 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!

  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)(today+1);

  19. Enumerated (cont.) • for(today=monday; • today <= 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 it’s called. • Let’s look at a simple example similar to the evaluate example in the text.

  21. 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)); }

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

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

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

  25. 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.)

  26. Simple version of fig 7.13 y (x1,y1) (x2,y2) y = f(x) (x3,y3) (x0,y0) (x4,y4) x x0=a 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.

More Related