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

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

View Full Document Right Arrow Icon
Use of scanf We have now discussed how to print out formatted information to the screen, but this isn't nearly as useful unless we can read in information from the user. (This is one way we can make a program more useful!) When you read in information from the keyboard, you MUST read it into a variable. In particular, you must read it into the address of the variable. (This is where the variable is stored in memory.) In order to do this, you must specify the type of information being read in, and the name of the variable you want the information read into. For example, the following line would read in an integer into the variable number (assuming it is already declared): scanf("%d", &number); The & in front of number means "address of." This simply says to store whatever the user enters into the memory address where number is stored. Leaving out the & will cause your program to work incorrectly! You may also read in multiple pieces of information at once. The following line reads in the month and day of birth into the integer variables month and day, (assuming they are already declared.) scanf("%d%d", &month, &day); When the user enters this information, they must separate what they enter with a space.
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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:
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2011 for the course COP 3223 taught by Professor Guha during the Spring '08 term at University of Central Florida.

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 Right Arrow Icon
Ask a homework question - tutors are online