1 / 19

C Fundamentals - I

B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Fa05: Timing about right. C Fundamentals - I. B Smith: Sp06: Used Dev-C for most things here and had students do the hands-on experimentation stuff. Rate: 3. Good, practical, programming.

dacian
Download Presentation

C Fundamentals - I

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. B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Fa05: Timing about right C Fundamentals - I B Smith: Sp06: Used Dev-C for most things here and had students do the hands-on experimentation stuff. Rate: 3. Good, practical, programming. Math 130Lecture # 2

  2. Overview • Define basic data type • Evaluate Arithmetic Expressions using C

  3. Basic data types in C • Integer: • 2, -50, +2, -2 • Floating point: • 0.331, -5978.55, 1.0, +1. • Double precision: • 6 – 7 digits precision • Character: • ‘W’, ‘w’, ‘+’, ‘&’ • Exponential Notation • 5.123e-7

  4. B Smith: This is a great opportunity to start using a debugger in class to show variable changes. This would be an alternative to using printf to trace through a program! Arithmetic Expressions • take arithmetic (numerical) values and • return an arithmetic (numerical) value • Are composed using the following operators: +(unary plus) -(unary minus) + (addition) - (subtraction) * (multiplication) / (division or quotient) % (modulus or remainder) L01a.c

  5. Unary operators • Called unary because they require one operand. • Examples i = +1; /* + used as a unary operator */ j = -i; /* - used as a unary operator */ • The unary operator ‘+’ does nothing • just emphasis that a numeric constant is positive. • The unary operator ‘–’ produces the negative of its operand.

  6. Precedence in Expressions • Defines the order in which an expression is evaluated • As in algebra we need rules regarding what gets done first • Write down your answers to the following:

  7. P.E.M.D.A.S. Precedence in Expressions 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) P stands for parentheses, E for Exponents, M for multiplication D for division, A for addition, and S for subtraction.

  8. B Smith: Provide an example for next slide. Your turn. .. More on precedence *, /, %are at the same level of precedence +, - are at the same level of precedence For operators at the same “level,” left-to-right ordering is applied. 2 + 3 – 1 = (2 + 3) – 1 = 4 2 – 3 + 1 = (2 – 3) + 1 = 0 2 * 3 / 4 = (2 * 3) / 4 = 6 / 4 2 / 3 * 4 = (2 / 3) * 4 = 0 / 4

  9. 6.2 B Smith: This entire sequence can be covered on 1 slide using a tablet Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)

  10. 6.2 Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)

  11. Integer division results in integer quotient Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)

  12. 7 Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) = 0

  13. 7 Precedence in Expressions – Example (cont) 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5)

  14. B Smith: Redundant since done on board with tablet. Consider writing out line 1 and finishing live int-s and float-s • float is a “communicable” type • Example: 1 + 2 * 3 - 4.0 / 5 = 1 + (2 * 3) - (4.0 / 5) = 1 + 6 - 0.8 = 6.2

  15. int-s and float-s – Example 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0

  16. int-s and float-s – Example 2 (cont) (1 + 2.0) * (3 - 4) / 5 = ((1 + 2.0) * (3 - 4)) / 5 = (3.0 * -1) / 5 = -3.0 / 5 = -0.6

  17. int-s and float-s – Example 3 (1 + 2.0) * ((3 - 4) / 5) = (1 + 2.0) * (-1 / 5) = 3.0 * 0 = 0.0

  18. Floating Point and Exponential Notation(e.g. 2.555e4, 3.1e-3, 7e0) • Generally written in exponential format • i.e., 3000 would be 3e3 and • 1/1000 or 0.001 would be 1e-3 • the precision of the floating-point representation is determined by the number of digits the computer will allow for the decimal part of the mantissa • i.e. in 2.555e4, 2.555 is the mantissa • Whereas the the range of the floating-point representation is based on the number of digits the computer allows for the exponent • i.e., in 2.555e4, 4 is the exponent

  19. Summary • Defined basic data type • Evaluated Arithmetic Expressions using C • Read chapter 2 of your book and review lectures notes

More Related