1107 Principles of Programming
Formatted Input
Facilitators
Mr. Luyima Alex Cedric
[email protected]
0772 302775 / 0708 045 305
Mr. Mugejjera Emmanuel
[email protected]Phone: 0703 186705 / 0772 959183
Mr. Mawebe John Bosco
[email protected]Phone: 0773-361111 / 0702 957911
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -1-
1.5.5 The printf Conversion Specifiers
When displaying numbers special care must be given to the data type. Each data type has to be used with
printf function in a specific format. In order to display the correct values using the printf function
conversion specifiers should be used. They are used to instruct the compiler about the type of numbers
appearing in the program, which in turn determines the suitable memory storage locations.
The conversion specifiers are also referred as format characters or format specifiers. Table 2.5 above
summarizes conversion specifies supported by the printf function. Consider the example given:
/* Program-2.5 */
#include<stdio.h>
int main( )
{
int a = 20;
float b = 20.0;
printf("%d\n",a);
printf("%f\n",b);
return 0;
}
The first number is of the type integer while the second number is of the type float. Conversion
specifier %d stands for decimal (whole number) while %f stands for float. Incorrect use of format
characters would result in wrong outputs.
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -2-
Consider Program-2.7 which makes use of variables.
/* Program-2.7*/
#include <stdio.h>
int main()
{
int a = 3;
float b = 10.0;
float c;
c = b/a;
printf("A is %d\n", a); //decimal value
printf("B is %f\n",b); //decimal value
printf("Answer is %f\n",c); //floating point value
return 0;
}
Executing Prog-2.7 will display the following on the screen:
In this program variables a and b are initialized with values 3 and 10.0 and the answer (b/a) is stored in
variable “c”.
In Program-2.7 you may wish to see the answer appearing in a more manageable form like 3.3 or 3.33
rather than 3.333333. This can be achieved by using modifiers along with the format characters in
order to specify the required field width.
Program-2.8 shows how digits to the right of the decimal point can be suppressed.
/* Program-2.8*/
#include <stdio.h>
int main()
{
int a = 3;
float b = 10.0;
float c;
c = b/a;
printf("A is %d \n", a); //decimal value
printf("B is %.1f \n", b); //decimal value
printf("Answer is %.2f \n", c); //float value
return 0;
}
Executing Prog-2.8 will display the following on the screen:
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -3-
The format %.0f will suppress all the digits to the right of the decimal point, while the format %.2f will
display first two digits after that decimal point.
1.5.6 ASCII (American Standard Code for Information Interchange)
ASCII (pronouncedask-ee), is a code for representing English characters as numbers, with each letter
assigned a number from 0 to 127. For example, the ASCII code for uppercase M is 77 and the ASCII
code for lowercase t is 116.
For a list of commonly used characters and their ASCII equivalents, refer to the ASCII table on the next
page. Most computers use ASCII codes to represent text, which makes it possible to transfer data from
one computer to another.
Table 2.6 –ASCII Table
1.5.6.1 Displaying Character Variables
Program-2.9 illustrates the use of character variables.
/* Program-2.9*/
#include <stdio.h>
int main()
{
char first_letter;
first_letter = ‘A’;
printf("Character: %c\n", first_letter); //display character
printf("ASCII value: %d\n", first_letter); //display ASCII
return 0;
}
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -4-
Executing Prog-2.9 will display the following:
Character: A
ASCII value: 65
1.5.7 Formatted Input
We have already used printf function to display formatted output. The scanf is a similar function that
is used to read data into a program. The scanf function accepts formatted input from the keyboard. In
Program-2.10, three integer variables are declared (a, b and sum). Values of a and b are to be read
from the keyboard using the scanf function, while the value of the variable sum is calculated as the
summation of a and b.
/* Program-2.10 */
#include <stdio.h>
int main()
{
int a, b, sum;
printf(“Enter the first number: ”);
scanf("%d", &a); //read 1st number
printf(“Enter the second number: ”);
scanf("%d", &b); // read 2nd number
sum = a + b; // calculates sum of a and b
printf("a+b =%d\n", sum); //display answer
return 0;
}
Sample Run
The scanf function uses the same set of formatting characters as the printf function to describe the type
of expected input i.e. %f for floats and %d for whole numbers. Program-2.10 shows the use of %d for
integer values and Program-2.11 shows the use of %f for floats.
Note that the scanf function uses the variables a and b as “&a” and “&b”. The symbol “&” is called
the address of operator. The string “&a” represents the memory address containing variable a and is
called a pointer. Pointers will be covered later in this course. The scanf function also supports mixed
types of input data. Consider the following line of code:
scanf("%d%f", &a,&b);
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -5-
The scanf function accepts variable “a” as an integer and “b” as a floating point number. In certain
cases you may want to separate your inputs using a comma (,) rather than using the Enter key or a
blank space. In such cases you must include the comma between format characters as:
scanf("%f,%f", &a,&b);
For such a program your inputs should be in the form:
2,3
One insufficiency of the scanf function is that it cannot display strings while waiting for user input. This
will require use of an additional function like the printf in order to display a message as a prompt for
the user reminding the required data item. (See Program-2.10 and Program-2.11 above). In Program-
2.10, the user has to be first prompted for input by the statement:
printf(“Enter the first number: ”);
END
THANK YOU
- Ndejje University - IT 1107 / CS 1107 Principles of Programming -6-