Input and Output Functions I
ObjectivesThis section we should have a clearer idea of one of C's:
input functions, called scanf
output functions, called printf
Notice the important words here: "as the program runs". You can already store values in variables using assignment. That is:
a=100;stores 100 in the variable a each time we run the program, no matter what we do. Without some sort of input command every program would produce exactly the same result every time it was run. This would certainly make debugging easy! But in practice, of course, we need programs to do different jobs each time they are run. There are a number of different C input commands, the most useful of which is the scanf command. To read a single integer value into the variable called:
scanf("%d",&a);When the program reaches the scanf statement it pauses to give the user time to type something on the keyboard and continues only when users press
The %d, both in the case of scanf and printf, simply lets the compiler know that the value being read in, or printed out, is a decimal integer - that is, a few digits but no decimal point.
The printf and scanf functions do differ from the sort of functions that you will created for yourself in that they can take a variable number of parameters. In the case of printf the first parameter is always a string (ex: "Hello World") but after that you can include as many parameters of any type that you want to. That is, the printf function is usually of the form:
printf(string,variable,variable,variable...)The string is all-important because it specifies the type of each variable in the list and how you want it printed. The string is usually called the control string or the format string. The way that this works is that printf scans the string from left to right and prints on the screen, or any suitable output device, any characters it encounters - except when it reaches a % character. The % character is a signal that what follows it is a specification for how the next variable in the list of variables should be printed. "printf uses this information to convert and format the value that was passed to the function by the variable and then moves on to process the rest of the control string and anymore variables it might specify". For example:
printf("Hello World");
only has a control string and, as this contains no % characters it results in Hello World being displayed and doesn't need to display any variable values. The specifier %d means convert the next value to a signed decimal integer and so:
printf("Total = %d",total);will print Total = and then the value passed by >total as a decimal integer.
The % Format Specifiers| Usual variable type | Display | |
|---|---|---|
| %c | char | single character |
| %d (%i) | int | signed integer |
| %e (%E) | float or double | exponential format |
| %f | float or double | signed decimal |
| %g (%G) | float or double | use %f or %e as required |
| %o | int | unsigned octal value |
| %p | pointer | address stored in pointer |
| %s | array of char | sequence of characters |
| %u | int | unsigned decimal |
| %x (%X) | int | unsigned hex value |