Difference between revisions of "ES101 - Lesson 3 : scanf"
(→scanf) |
(→Assignment) |
||
Line 49: | Line 49: | ||
== Assignment == | == Assignment == | ||
− | To provide input to your Board from your computer's PC, you need to open Hercules Program at 38400bps by going to the Serial Tab. | + | To provide input to your Board from your computer's PC, you need to open '''Hercules''' Program at 38400bps by going to the Serial Tab. To enter input, you can simply type on the '''Hercules''' window and hit the Enter key to enter text from your keyboard, which will be transmitted to the Board. Also note that after you open the COM Port in Hercules, the reset button should be pressed on the Board to restart the program. |
# Declare four variables and choose their appropriate type, such as int, or float. Use a meaningful name based on the following uses: | # Declare four variables and choose their appropriate type, such as int, or float. Use a meaningful name based on the following uses: |
Revision as of 00:22, 18 September 2012
scanf
The scanf function can be used to input information to your program variables. Here is an overview:
- Compile and load your program
- Open Hercules and go to Serial tab.
- Right click in the middle of the screen and double check the following items are checked:
- CR/LF Enable
- Local Echo
scanf syntax is similar to printf, but it scans for input. The specifier list is the same, and one or more inputs can be captured similarly to how you use printf. Let's start with a code example:
int age;
float height;
printf("What is your age? ");
scanf("%i", &age);
printf("What is your height? ");
scanf("%f", &height);
Notice the following differences and similarities of scanf vs.printf
- scanf uses same specifiers,%i,%f etc.
- scanf variables after the double quotes have & symbol.
The & (ampersand) symbol is needed because it is called the address operator. Instead of passing the variable's value, you pass the variable's address such that scanf can input the data into the memory location given by the variable's address. If you forget to include the & operator, you will experience unexpected results because scanf would probably input the data at the wrong memory location.
scanf should be intuitive to understand except when you try to scanf a single character (%c). What happens is that a previous scanf does not consume the <Enter key> character (0x0A) and the next scanf picks up this character when you try to use: scanf("%c", &my_char);
. To solve this problem a call to getchar() is made, which discards this unconsumed hanging character, and scanf will wait for a new character from Hercules terminal.
int age;
char my_char;
printf("What is your age? ");
scanf("%i", &age);
/**
* This code will not function unless you insert:
* getchar(); before the next scanf() statement
*/
printf("Press any key: ");
scanf("%c", &my_char);
printf("You entered: %c which is the number %u\n", my_char, my_char);
Assignment
To provide input to your Board from your computer's PC, you need to open Hercules Program at 38400bps by going to the Serial Tab. To enter input, you can simply type on the Hercules window and hit the Enter key to enter text from your keyboard, which will be transmitted to the Board. Also note that after you open the COM Port in Hercules, the reset button should be pressed on the Board to restart the program.
- Declare four variables and choose their appropriate type, such as int, or float. Use a meaningful name based on the following uses:
- A variable to store the age (in years) of the user
- A variable to store the age in months.
- A variable to store the height in inches of the user.
- A variable to store the height in centimeters.
- Ask the user to input their age in years and store it to your age variable.
- Ask the user to input their height and store it to your height variable in inches.
- Calculate the age in months and store it to the appropriate variable.
- Calculate the height in centimeters (2.54 cm per inch) and store it to the appropriate variable.
- Output the user's age in months and years, height in inches, and height in centimeters.
- The height in centimeters should only display 2 decimal digits.
Questions
- After completing the assignment, try removing & symbol for the scanf statement that asks for user's height.
- What is the program output? Does it work as expected?
- Try getting a single character at the end of your program and simply output what the user inputs.
- Does the program work as expected?