8 Displaying Prompts When input data is needed in an interactive program you

# 8 displaying prompts when input data is needed in an

This preview shows page 8 - 17 out of 25 pages.

8 Displaying Prompts When input data is needed in an interactive program, you should use the printf function to display a prompting message , or prompt , that tells the user what data to enter. Printf(“Enter the distance in miles> “);
9 The scanf Function scanf(“%lf”, &miles); function name function arguments format string variable list place holder  When user inputs a value, it is stored in variable miles . The placeholder type tells the function what kind of data to store into variable miles. The & is the C address of operator. The & operator in front of variable miles tells the scanf function the location of variable miles in memory.
10 Fig 2.6: Scanning data line Bob char letter_1, letter_2, letter_3; …. Scanf(“%c%c%c”, &letter_1, &letter_2, &letter_3);
11 Assignment Statements Assignment statement - Stores a value or a computational result in a variable kms = KMS_PER_MILE * miles; The assignment statement above assigns a value to the variable kms . The value assigned is the result of the multiplication of the constant KMS_PER_MILE by the variable miles .
12 Figure 2.3 Effect of kms = KMS_PER_MILE * miles;
13 Figure 2.2 Memory(a) Before and (b) After Execution of a Program
14 More on Assignments In C the symbol = is the assignment operator Read it as ”becomes”, ”gets”, or ”takes the value of” rather than ”equals” because it is not equivalent to the equal sign of mathematics. In C, == tests equality. In C you can write assignment statements of the form: sum = sum + item; where the variable sum appears on both sides of the assignment operator. This is obviously not an algebraic equation, but it illustrates a common programming practice. This statement instructs the computer to add the current value of sum to the value of item ; the result is then stored back into sum .
15 return Statement return (0); Transfers control from your program to the operating system. return (0) returns a 0 to the Operating System and indicates that the program executed without error. It does not mean the program did what it was suppose to do. It only means there were no syntax errors. There still may have been logical errors. Once you start writing your own functions, you’ll use the return statement to return information to the caller of the function.
16 Reserved Words /* Converts distances from miles to kilometers */ #include <stdio.h> /* printf, scanf definitions */ #define KMS_PER_MILE 1.609

#### You've reached the end of your free preview.

Want to read all 25 pages?

• Spring '10
• baleh
• miles, printf