Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input
  2. 2. Objectives <ul><li>Assignment </li></ul><ul><li>Mathematical Library Functions </li></ul><ul><li>Interactive Input </li></ul><ul><li>Formatted Output </li></ul>
  3. 3. Objectives (continued) <ul><li>Symbolic Constants </li></ul><ul><li>Case Study: Interactive Input </li></ul><ul><li>Common Programming and Compiler Errors </li></ul>
  4. 4. Assignment <ul><li>The general syntax for an assignment statement is </li></ul><ul><ul><li>variable = operand; </li></ul></ul><ul><ul><li>The operand to the right of the assignment operator (=) can be a constant, a variable, or an expression </li></ul></ul><ul><li>The equal sign in C does not have the same meaning as an equal sign in algebra </li></ul><ul><ul><li>length=25; is read “length is assigned the value 25” </li></ul></ul><ul><li>Subsequent assignment statements can be used to change the value assigned to a variable </li></ul><ul><ul><ul><li>length = 3.7; </li></ul></ul></ul><ul><ul><ul><li>length = 6.28; </li></ul></ul></ul>
  5. 5. Assignment (continued) <ul><li>The operand to the right of the equal sign in an assignment statement can be a variable or any valid C expression </li></ul><ul><ul><li>sum = 3 + 7; </li></ul></ul><ul><ul><li>product = .05 * 14.6; </li></ul></ul><ul><ul><ul><li>The value of the expression to the right of = is computed first and then the calculated value is stored in the variable to the left of = </li></ul></ul></ul><ul><li>Variables used in the expression to the right of the = must be initialized if the result is to make sense </li></ul><ul><li>amount + 1892 = 1000 + 10 * 5 is invalid! </li></ul>
  6. 6. Assignment (continued) If width was not initialized, the computer uses the value that happens to occupy that memory space previously (compiler would probably issue a warning)
  7. 7. Assignment (continued) <ul><li>= has the lowest precedence of all the binary and unary arithmetic operators introduced in Section 2.4 </li></ul><ul><li>Multiple assignments are possible in the same statement </li></ul><ul><ul><li>a = b = c = 25; </li></ul></ul><ul><ul><ul><li>All = operators have the same precedence </li></ul></ul></ul><ul><ul><ul><li>Operator has right-to-left associativity </li></ul></ul></ul><ul><ul><ul><ul><li>c = 25; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>b = c; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>a = b; </li></ul></ul></ul></ul>
  8. 8. Implicit Type Conversions <ul><li>Data type conversions take place across assignment operators </li></ul><ul><ul><li>double result; </li></ul></ul><ul><ul><li>result = 4; //integer 4 is converted to 4.0 </li></ul></ul><ul><li>The automatic conversion across an assignment operator is called an implicit type conversion </li></ul><ul><ul><li>int answer; </li></ul></ul><ul><ul><li>answer = 2.764; //2.764 is converted to 2 </li></ul></ul><ul><ul><li>Here the implicit conversion is from a higher precision to a lower precision data type; the compiler will issue a warning </li></ul></ul>
  9. 9. Explicit Type Conversions (Casts) <ul><li>The operator used to force the conversion of a value to another type is the cast operator </li></ul><ul><ul><li>(dataType) expression </li></ul></ul><ul><ul><ul><li>where dataType is the desired data type of the expression following the cast </li></ul></ul></ul><ul><li>Example: </li></ul><ul><ul><li>If sum is declared as double sum; , (int) sum is the integer value determined by truncating sum ’s fractional part </li></ul></ul>
  10. 10. Assignment Variations sum = sum + 10 is not an equation—it is an expression that is evaluated in two major steps
  11. 11. Assignment Variations (continued)
  12. 12. Assignment Variations (continued)
  13. 13. Assignment Variations (continued) <ul><li>Assignment expressions like sum = sum + 25 can be written using the following operators: </li></ul><ul><ul><li>+= -= *= /= %= </li></ul></ul><ul><li>sum = sum + 10 can be written as sum += 10 </li></ul><ul><li>price *= rate is equivalent to price = price * rate </li></ul><ul><li>price *= rate + 1 is equivalent to price = price * (rate + 1) </li></ul>
  14. 14. Accumulating <ul><li>The first statement initializes sum to 0 </li></ul><ul><ul><li>This removes any previously stored value in sum that would invalidate the final total </li></ul></ul><ul><ul><li>A previously stored number, if it has not been initialized to a specific and known value, is frequently called a garbage value </li></ul></ul>
  15. 15. Accumulating (continued)
  16. 16. Accumulating (continued)
  17. 17. Counting <ul><li>A counting statement is very similar to the accumulating statement </li></ul><ul><ul><li>variable = variable + fixedNumber; </li></ul></ul><ul><li>Examples: i = i + 1; and m = m + 2; </li></ul><ul><li>Increment operator (++): variable = variable + 1 can be replaced by variable++ or ++variable </li></ul>
  18. 18. Counting (continued)
  19. 19. Counting (continued)
  20. 20. Counting (continued) <ul><li>When the ++ operator appears before a variable, it is called a prefix increment operator ; when it appears after a variable, it is called postfix increment operator </li></ul><ul><ul><li>k = ++n; is equivalent to </li></ul></ul><ul><ul><ul><li>n = n + 1; // increment n first </li></ul></ul></ul><ul><ul><ul><li>k = n; // assign n's value to k </li></ul></ul></ul><ul><ul><li>k = n++; is equivalent to </li></ul></ul><ul><ul><ul><li>k = n; // assign n's value to k </li></ul></ul></ul><ul><ul><ul><li>n = n + 1; // and then increment n </li></ul></ul></ul>
  21. 21. Counting (continued) <ul><li>Prefix decrement operator: the expression k = --n first decrements the value of n by 1 before assigning the value of n to k </li></ul><ul><li>Postfix decrement operator: the expression k = n-- first assigns the current value of n to n and then reduces the value of n by 1 </li></ul>
  22. 22. Counting (continued)
  23. 23. Mathematical Library Functions
  24. 24. Mathematical Library Functions (continued) <ul><li>The argument to sqrt must be floating-point value; passing an integer value results in a compiler error </li></ul><ul><ul><li>Return value is double-precision </li></ul></ul><ul><li>Must include #include <math.h> </li></ul>
  25. 25. Mathematical Library Functions (continued)
  26. 26. Mathematical Library Functions (continued)
  27. 27. Mathematical Library Functions (continued) Argument need not be a single constant
  28. 28. Mathematical Library Functions (continued) <ul><li>The step-by-step evaluation of the expression </li></ul><ul><ul><li>3.0 * sqrt(5 * 33 - 13.91) / 5 </li></ul></ul><ul><li>is (see next slide) </li></ul>
  29. 29. Mathematical Library Functions (continued)
  30. 30. Mathematical Library Functions (continued) <ul><li>Determine the time it takes a ball to hit the ground after it has been dropped from an 800-foot tower </li></ul><ul><ul><li>time = sqrt(2 * distance/g), where g = 32.2 ft/sec2 </li></ul></ul>
  31. 31. Mathematical Library Functions (continued)
  32. 32. Interactive Input
  33. 33. Interactive Input (continued) <ul><li>This program must be rewritten to multiply different numbers </li></ul><ul><li>scanf() is used to enter data into a program while it is executing; the value is stored in a variable </li></ul><ul><ul><li>It requires a control string as the first argument inside the function name parentheses </li></ul></ul>
  34. 34. Interactive Input (continued) <ul><li>The control string passed to scanf() typically consists of conversion control sequences only </li></ul><ul><li>scanf() requires that a list of variable addresses follow the control string </li></ul><ul><ul><li>scanf(&quot;%d&quot;, &num1); </li></ul></ul>
  35. 35. Interactive Input (continued)
  36. 36. Interactive Input (continued) This statement produces a prompt Address operator (&)
  37. 37. Interactive Input (continued) <ul><li>scanf() can be used to enter many values </li></ul><ul><ul><li>scanf(&quot;%f %f&quot;,&num1,&num2); //&quot;%f%f&quot; is the same </li></ul></ul><ul><li>A space can affect what the value being entered is when scanf() is expecting a character data type </li></ul><ul><ul><li>scanf(&quot;%c%c%c&quot;,&ch1,&ch2,&ch3); stores the next three characters typed in the variables ch1 , ch2 , and ch3 ; if you type x y z , then x is stored in ch1 , a blank is stored in ch2 , and y is stored in ch3 </li></ul></ul><ul><ul><li>scanf(&quot;%c %c %c&quot;,&ch1,&ch2,&ch3); causes scanf() to look for three characters, each character separated by exactly one space </li></ul></ul>
  38. 38. Interactive Input (continued) <ul><li>In printing a double-precision number using printf() , the conversion control sequence for a single-precision variable, %f , can be used </li></ul><ul><li>When using scanf() , if a double-precision number is to be entered, you must use the %lf conversion control sequence </li></ul><ul><li>scanf() does not test the data type of the values being entered </li></ul><ul><li>In scanf(&quot;%d %f&quot;, &num1, &num2) , if user enters 22.87 , 22 is stored in num1 and .87 in num2 </li></ul>
  39. 39. Caution: The Phantom Newline Character
  40. 40. Caution: The Phantom Newline Character (continued) <ul><li>The following is a sample run for Program 3.10: </li></ul><ul><ul><li>Type in a character: m </li></ul></ul><ul><ul><li>The keystroke just accepted is 109 </li></ul></ul><ul><ul><li>Type in another character: The keystroke just accepted is 10 </li></ul></ul>
  41. 41. Caution: The Phantom Newline Character (continued)
  42. 42. Caution: The Phantom Newline Character (continued)
  43. 43. A First Look at User-Input Validation
  44. 44. A First Look at User-Input Validation (continued) <ul><li>As written, Program 3.12 is not robust </li></ul><ul><li>The problem becomes evident when a user enters a non-integer value </li></ul><ul><ul><li>Enter three integer numbers: 10 20.68 20 </li></ul></ul><ul><ul><li>The average of 10, 20, and -858993460 is -286331143.333333 </li></ul></ul><ul><li>Handling invalid data input is called user-input validation </li></ul><ul><ul><li>Validating the entered data either during or immediately after the data have been entered </li></ul></ul><ul><ul><li>Providing the user with a way of reentering any invalid data </li></ul></ul>
  45. 45. Formatted Output <ul><li>6 </li></ul><ul><li>18 </li></ul><ul><li>124 </li></ul><ul><li>--- </li></ul><ul><li>148 </li></ul>Output is not aligned
  46. 46. Formatted Output (continued) 6 18 124 --- 148 Field width specifier
  47. 47. Formatted Output (continued)
  48. 48. Format Modifiers <ul><li>Left justification: printf(&quot;%-10d&quot;,59); produces the display 59 ٨٨٨٨٨٨٨٨ </li></ul><ul><li>Explicit sign display: printf(&quot;%+10d&quot;,59); produces the display ٨٨٨٨٨٨٨ + 59 </li></ul><ul><li>Format modifiers may be combined </li></ul><ul><ul><li>%-+10d would cause an integer number to both display its sign and be left-justified in a field width of 10 spaces </li></ul></ul><ul><ul><ul><li>The order of the format modifiers is not critical </li></ul></ul></ul><ul><ul><ul><ul><li>%+-10d is the same </li></ul></ul></ul></ul>
  49. 49. Other Number Bases [Optional] <ul><li>The decimal (base 10) value of 15 is 15. </li></ul><ul><li>The octal (base 8) value of 15 is 17. </li></ul><ul><li>The hexadecimal (base 16) value of 15 is f. </li></ul>
  50. 50. Other Number Bases (continued)
  51. 51. Other Number Bases (continued) <ul><li>The decimal value of the letter a is 97. </li></ul><ul><li>The octal value of the letter a is 141. </li></ul><ul><li>The hex value of the letter a is 61. </li></ul>
  52. 52. Other Number Bases (continued)
  53. 53. Symbolic Constants <ul><li>Literal data refers to any data within a program that explicitly identifies itself </li></ul><ul><li>Literal values that appear many times in the same program are called magic numbers </li></ul><ul><li>C allows you to define the value once by equating the number to a symbolic name </li></ul><ul><ul><li>#define SALESTAX 0.05 </li></ul></ul><ul><ul><li>#define PI 3.1416 </li></ul></ul><ul><ul><li>Also called symbolic constants and named constants </li></ul></ul>
  54. 54. Symbolic Constants (continued) # sign is a signal to a C preprocessor
  55. 55. Case Study: Interactive Input
  56. 56. Case Study: Interactive Input (continued)
  57. 57. Common Programming Errors <ul><li>Forgetting to assign initial values to all variables before the variables are used in an expression </li></ul><ul><li>Calling sqrt() with an integer argument </li></ul><ul><li>Forgetting to use the address operator, &, in front of variable names in a scanf() function call </li></ul><ul><li>Not including the correct control sequences in scanf() function calls for the data values that must be entered </li></ul><ul><li>Including a message within the control string passed to scanf() </li></ul>
  58. 58. Common Programming Errors (continued) <ul><li>Terminating a #define command to the preprocessor with a semicolon </li></ul><ul><li>Placing an equal sign in a #define command when equating a symbolic constant to a value </li></ul><ul><li>Using the increment and decrement operators with variables that appear more than once in the same expression </li></ul><ul><li>Being unwilling to test a program in depth </li></ul>
  59. 59. Common Compiler Errors
  60. 60. Common Compiler Errors (continued)
  61. 61. Summary <ul><li>Arithmetic calculations can be performed using assignment statements or mathematical functions </li></ul><ul><li>The assignment symbol, =, is an operator </li></ul><ul><li>C provides the +=, -=, *= and /= assignment operators </li></ul><ul><li>The increment operator, ++, adds 1 to a variable </li></ul><ul><li>The decrement operator, --, subtracts 1 from a variable </li></ul><ul><li>C provides library functions for calculating square root, logarithmic, and other mathematical computations </li></ul>
  62. 62. Summary (continued) <ul><li>Mathematical functions may be included within larger expressions </li></ul><ul><li>scanf() is a standard library function used for data input </li></ul><ul><li>When a scanf() function is encountered, the program temporarily suspends further statement execution until sufficient data has been entered for the number of variable addresses contained in the scanf() function call </li></ul>
  63. 63. Summary (continued) <ul><li>It is good programming practice to display a message, prior to a scanf() function call, that alerts the user as to the type and number of data items to be entered </li></ul><ul><li>Field width specifiers can be included with conversion control sequences to explicitly specify the format of displayed fields </li></ul><ul><li>Each compiled C program is automatically passed through a preprocessor </li></ul><ul><li>Expressions can be made equivalent to a single identifier using the preprocessor #define command </li></ul>