| City & Guilds 7261/224 | Coding and Programming in C II  |
| 1 | INTRODUCTION
The scanf("format-string", arguments);
Format-string contains ordinary characters, which the input
must match, and conversion specifications which cause input
fields (see Section 2.1) to be converted and assigned to the
variables, whose addresses are supplied in
arguments. Format-string can also contain whitespace
characters which tells scanf to ignore whitespace in the
input.
| ||||||||||||||
| 2 | CONVERSION SPECIFICATION
A simplified syntax for the conversion specification is &typeFor the purposes of City & Guilds (C&G Ref: 7261/224, Section 6.1.2) you need to know only how to input numeric variables. This is done using the d and f types:
| ||||||||||||||
| 2.1 | Definition of an Input Field
An input field is defined as a string of characters delimited by
whitespace. This means that
| ||||||||||||||
| 3 | EXAMPLE
/* main: ask for three integers then display their sum */
int main(void)
{
int i1, i2, i3, sum;
printf("Enter three integers: ");
scanf("%d %d %d", &i1, &i2, &i3);
sum = i1 + i2 + i3;
printf("The sum is %d\n", sum);
return 0;
} /* main */
Four integer variables are declared.
The next line adds the variables together and assigns the result
to Note well: in | ||||||||||||||
| 1 | Type in and run the above example program. When entering the
three integer values, use the input field delimiters space, tab, and newline.
| |
| 2 | Write a program, using the scanf function, which
assigns numbers to the integer variables days,
hours, and minutes and then calculates and
displays the total number of minutes involved. Your program should
display 5164 when 3 14 4 is entered. More
obvious test data include 4 when 0 0 4
entered; 60 when 0 1 0 entered; and
1440 when 1 0 0 entered. Don't worry about
input validation 2. |
|
| 3 | Write a program, using the scanf function, which
converts Fahrenheit to Celsius. Use the formula C = 5(F
- 32) / 9. Prompt the user with a suitable request, and display the
calculated Celsius value to two decimal places. Use * for
multiply, and / for divide. Afterwards, try using
C = (5 / 9) * (F - 32). Why do you think it doesn't work?
|