DATA INPUT & OUTPUT
DR. THERESA-SAMUELLE ADJAIDOO
2
“ Testing leads to failure, and
failure leads to understanding.
BURT RUTAN ”
C PROGRAMMING - DATA INPUT AND OUTPUT
3
OUTLINE • DATA INPUT/OUTPUT
C PROGRAMMING - DATA INPUT AND OUTPUT
4
DATA INPUT/OUTPUT
From the very first program, to some extent, it is
understood by what data output refers to; displaying
data to the user.
The reverse; getting data from the user, can also be
described as data input.
With the help of functions in the stdio.h file, these
operations are made possible.
C PROGRAMMING - DATA INPUT AND OUTPUT
5
DATA INPUT/OUTPUT
There are six main functions in the stdio.h that would be
of immense use in this course.
They are: printf(), scanf(), putchar(), getchar(), puts()
and gets()
The general syntax and mode of usage of each of
these would be explained in the following slides.
C PROGRAMMING - DATA INPUT AND OUTPUT
6
PRINTF
The printf() function, as seen already, writes output
data from the computer to a standard output device.
This function can be used to output any combination of
numerical values, single characters and strings.
Strings are simply a concatenation of characters.
C PROGRAMMING - DATA INPUT AND OUTPUT
7
PRINTF
The general syntax for using printf is :
printf(“control string”, arg1, arg2,…, argn);
where control string refers to a string containing certain formatting
information, and arg_1, arg_2,…, arg_n are arguments that represent the
individual output data items.
C PROGRAMMING - DATA INPUT AND OUTPUT
8
PRINTF
Control strings are also known as string modifiers/formatters/specifiers.
While using printf(), these string formatters have the ability to specify how
values from variables are displayed.
The next slide presents some of these control strings.
C PROGRAMMING - DATA INPUT AND OUTPUT
CONTROL STRINGS
C PROGRAMMING - DATA INPUT AND OUTPUT
9
10
PRINTF
However, printf() statements do not always contain
control strings.
For example, what was written in the first program.
printf(“Hello, World!\n”);
printf(“I am 50 years old”);
C PROGRAMMING - DATA INPUT AND OUTPUT
11
PRINTF
The above examples would print exactly what has been capsuled in the
quotation marks (together with the effect of any escape sequences).
However, there are times we would want to print values that are stored in
variables (memory).
To do so we would have to use control strings.
C PROGRAMMING - DATA INPUT AND OUTPUT
12
PRINTF
For example:
int age = 50; //the variable is age and the value stored is 50
printf(“%d \n”, age); // to print the value 50 as an integer
printf(“I am %d years old”, age); // printing the age from memory
C PROGRAMMING - DATA INPUT AND OUTPUT
13
PRINTF
For example:
int count = 4; //the variable is count and the value stored is 4
float weight = 90.61;
printf(“Angela who bought %d oranges weighs %g kilograms”,
count, weight);
// combining various variables in one print statement
C PROGRAMMING - DATA INPUT AND OUTPUT
14
PRINTF
In the last example, a number of variables were
combined in one printf() statement.
It is important to take close notice of how the control
strings and their respective variables are ordered.
Misplacing them would not produce the desired
output.
C PROGRAMMING - DATA INPUT AND OUTPUT
15
PRINTF
The control strings can also be used to specify the
length of value to be displayed as well as how many
decimal points a floating point has.
Find out more on how these are done.
C PROGRAMMING - DATA INPUT AND OUTPUT
16
SCANF
Input data can be entered into the computer from a
standard input device by means of a C library function
by name scanf().
This function can be used to input any combination of
numerical values, single characters and strings.
C PROGRAMMING - DATA INPUT AND OUTPUT
17
SCANF
The general syntax for using scanf is:
scanf(“control string”, arg_1, arg_2,…, arg_n);
where control string refers to a string containing certain
formatting information, and arg_1, arg_2,…, arg_n are
arguments that represent the individual input data items.
C PROGRAMMING - DATA INPUT AND OUTPUT
18
SCANF
A lot has already been said on control strings. So it’s
okay to start taking examples.
Imagine if you were to take the age from a user. It can
be done doing the following.
int personAge;
scanf(“%d”, &personAge);
C PROGRAMMING - DATA INPUT AND OUTPUT
19
SCANF
We first declared the integer variable personAge which
would hold the person’s age.
Then using the scanf() statement we patiently wait for
the person to enter the age and store the age in the
declared variable called personAge.
So when the compiler gets to the line with the scanf()
statement it would keep blinking a cursor until the
person enters a value
C PROGRAMMING - DATA INPUT AND OUTPUT
20
SCANF
The entered value would be treated as an integer
because we specified %d as the control string.
The entered value is stored in the variable personAge.
Once stored it can always be retrieved later in the
program.
C PROGRAMMING - DATA INPUT AND OUTPUT
21
& - THE “ADDRESS OF” OPERATOR
You must have been wondering why we placed an
ampersand (&) before the variable personAge when
using the scanf statement.
This ampersand is known as the “address of” operator
and it is used for accessing the address (memory
location) of the declared variable where we want to
store the value.
C PROGRAMMING - DATA INPUT AND OUTPUT
22
& - THE “ADDRESS OF” OPERATOR
Without the ampersand (&) the scanf statement would
not work as we want it to.
The statement &personAge would provide the compiler
with the address of the variable personAge.
This address of operator can also be used in the printf
statement to print out the memory addresses of
variables using the control string %p. More of this would
be covered later when treating the topic Pointers.
C PROGRAMMING - DATA INPUT AND OUTPUT
23
SCANF – MULTIPLE USER INPUT
There are times when there would be the need of
taking multiple user inputs.
For example taking 3 exam scores from the user in order
to compute the average score.
There are two ways of doing this. The next few slides
would demonstrate this.
C PROGRAMMING - DATA INPUT AND OUTPUT
24
SCANF – MULTIPLE USER INPUT
One way of getting it done is by writing the scanf
statement each time a value is to be taken. For
example:
float score_1, score_2, score_3;
scanf(“%g”, &score_1);
scanf(“%g”, &score_2);
scanf(“%g”, &score_3);
C PROGRAMMING - DATA INPUT AND OUTPUT
25
SCANF – MULTIPLE USER INPUT
Another way of getting it done is by writing the scanf
statement just once, while comma-separating the
arguments. For example:
float score_1, score_2, score_3;
scanf(“%g %g %g”, &score_1, &score_2, &score_3);
It should however be noted that the control strings are
not comma-separated.
C PROGRAMMING - DATA INPUT AND OUTPUT
26
GETCHAR AND PUTCHAR
getchar() and putchar() are also functions in the
standard input and output header file which work
mainly with characters.
getchar() is responsible for taking a character value
from a user (input) and putchar() is responsible for
displaying a character value to a user.
C PROGRAMMING - DATA INPUT AND OUTPUT
27
GETCHAR AND PUTCHAR
The syntax for getchar() is
declared character variable = getchar();
For example
char choice = getchar();
Since it is an input function, this would cause the
compiler to patiently wait while blinking a cursor until
the user enters a character value which would be
stored in the variable choice.
C PROGRAMMING - DATA INPUT AND OUTPUT
28
GETCHAR AND PUTCHAR
The syntax for putchar() is
putchar(character variable);
For example
putchar(choice);
Since it is an output function, this would cause the
compiler to display the character value which was
stored in the variable choice.
C PROGRAMMING - DATA INPUT AND OUTPUT
29
GETS AND PUTS
The gets() and puts() functions are also input and
output functions targeted mainly for string values.
How they are used would be covered later when
treating the topic Strings.
C PROGRAMMING - DATA INPUT AND OUTPUT
30
THE END
Any Questions?
Contact: [email protected] or [email protected]
C PROGRAMMING - DATA INPUT AND OUTPUT