- Now that we know what strings are
and how they are stored,
let's see additional uses for a string
when it comes to interacting
with a computer via the screen,
what we call Input/Output.
What is Input?
It's essentially a series of bytes of data
that are entered via the screen, keyboard or network.
Each time you type something with a network,
the information travels from computer to computer.
You type something on a keyboard,
the information is going
from your keyboard onto the computer,
and it's being stored somewhere in memory.
The computer, the compiler, the language,
that's a whole system that is tasked
with processing the data that you are entering.
Conversely, when the computer wants
to present data to you,
it sends a stream of bytes from memory to a device.
Most likely, it's going to be a screen,
but often can be a printer or some disk
or another storage device.
C provides a standard Input/Output library
which we already see included in your C program
called STDIO.H, and it has a function
that allows you to input and read information,
to and from the screen.
Let's look at some of the main functionalities
of the Input/Output system in C.
We have one command which we have used already
so far in this class.
It provides powerful formatting features
to display multiple types of data on the screen.
Please recall that we looked at doubles, floats and ints,
by providing different directives to the PrintF Command.
ScanF is the counterpart to PrintF.
It's the main vehicle, not the only one,
that provides you the mechanism
to read data from the screen into the computer memory.
Both ScanF and PrintF offer broad conversion capability
across data types which means
you can read one type, format in a certain way,
and then forward it for storage in a different way,
as we will see in the upcoming videos.
Let's look at this program.
Notice in Line 11, the include STDIO.H.
This instructs this particular program
to use the extended library that includes
all the standard input/output features.
Also notice on Line 16,
we have defined two characters arrays,
which are Name to hold 20 locations,
and Address to hold 30 locations.
Let's see what are we going to do with those?
Lines 18 through 20 are the essential way
to prepare the computer for communicating with the user.
First, look at Line 19.
Nineteen prints a message to the user
instructing the user to choose a number.
Line 20 uses a new command, called Getchar,
which as its name implies, asks the user for a character.
Recall from our discussion on various data types,
that character is actually an integer,
despite its connotations to being a letter.
The character that's being captured by the function Getchar,
is simply stored in a variable C
which is defined in Line 18 of type character.
Line 19 provides the means for you
to communicate to the computer a number.
That number will be stored in the computer memory.
Similarly, Line 20 and 23 instructs the user
to enter an additional string,
except now in Line 23,
we are using the new command, called ScanF.
Notice that the ScanF directive, percent S,
and then the variable name.
Line 16 we declared an array of size 20, called Name.
We are now using this array.
However, recall that, that array
is a beginning of a string.
When using ScanF, we don't have to give it
the entire 20 spaces.
All we have to tell ScanF is,
there is such a thing called Name.
It starts at the particular label,
and the ampersand sign preceding the letter N in Name,
is another directive that the compiler understands,
that this Name is actually an address.
It's a labeled location in memory
in which the string entered in response
to the request in Line 22 will be stored.
Let's run this program and then we'll go back to the code
and learn some more of what it's doing.
As evident from Line 19, now I'm required to type something,
and I'm required to choose a number between one and five.
I'm going to choose three.
Now, I'm expected to type in a string.
I'm going to type my name, and I'm going to type my address.
I'm just going to make something up,
and the computer diligently
replies back to me, everything I typed.
My name, the address, however note this,
that it didn't put the entire address that I enter.
Why is that?
Notice that there is a space between 123 and Some Street.
Whenever the ScanF Command encountered the space,
it assumes I had finished entering my input
and therefore, it ignored everything
that comes after the space.
Gradually, we will improve our understanding
of how PrintF and ScanF work,
and we'll be able to create more powerful interactions
between user and computer.
Line 25 and 26 are similar to Line 22 and 23,
in the sense that they ask the user
to enter a string value which will be stored
in an array starting with the label as indicated.
Finally, Line 32 is the Putchar Command,
which is the counterpart of Getchar.
Essentially, the value stored in the C variable,
is now being displayed back to the user.
So far, this code presented a very simple,
elegant way, to ask the user
to provide numeric textural input,
storing it in memory, whether it's a variable
or an array of strings, array of characters,
and then displaying it back to the user on the screen.