{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

IO - Use of scanf We have now discussed how to print out...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Let's use the added feature of being able to read in information from the user to edit our second program: /* Arup Guha My Second C Program, edited 9/2/03 Computes the number of feet user ran. */ #include <stdio.h> #define YARDS_IN_MILE 1760 #define FEET_IN_YARD 3 int main(void) { int feet_in_mile, num_miles; feet_in_mile = YARDS_IN_MILE*FEET_IN_YARD; printf("How many miles did you run?\n"); scanf("%d", &num_miles); printf("You ran %d feet.\n", feet_in_mile*num_miles); return 0; } This program runs and works as before computing the variable feet_in_mile. The picture after this second line of code looks like: feet_in_mile num_miles 5280 Thus, num_miles is currently uninitialized.
Now, the user will be prompted with a message to enter the number of miles they ran. The scanf statement will WAIT until the user enters some information and hits enter. After this is done, the value entered by the user will be stored in the variable num_miles. Note: If the user doesn't enter the proper type of information, then the behavior of the scanf may not be as intended. A good exercise would be to try running a program that is expecting an integer input and actually enter a floating point number or a string and see what happens. Let's say that the user entered 3, now our picture looks like:

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 11

IO - Use of scanf We have now discussed how to print out...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online