Hello, and welcome.
In this lecture, I want to talk about reading input from the keyboard.
This is a basic concept that I wanted you guys to learn early
because it's very valuable and a lot of programs take input.
So I provided this early on.
Some of the concepts are not going to be completely clear.
But we'll definitely learn these concepts in other sections as we
go through, but it's important to learn this very early, and it's
very similar to the printf function.
So we've discussed output.
We know how to format output using printf.
We now have to learn a little bit about input, getting input
from the user essentially.
It's very useful to ask the user to enter data into a program.
We talked about command line arguments entering data via that
way, that's one way to do it.
Another way is to ask the user directly so that they can type
something in the keyboard.
So you get the input from the keyboard, but it gets
displayed you enter the input via the terminal the console.
That's how you enter the input.
The c library contains several input functions.
We're going to use scanf.
That's the most generic function.
It's very useful.
It provides different formats that you can enter.
So you can enter text and then it will get converted to different formats.
So scanf reads the input from the standard input stream, that's stdin.
Scanf can actually be used for reading from files as well if
the input stream is a file.
But we're not going to worry about any of that for now,
we're just going to use it to read input from the keyboard.
Okay, and so stdin by default is the keyboard.
Stdin is a stream.
Streams and c, three's different streams.
The error stream, standard in and standard output.
Standard in by default is the keyboard.
So we're going to keep it that way.
And using scanf it's going to read from standard in and then
based on what you enter or how you want to store the data, I
should say, it's going to convert what you entered because what
you enter is going to be text.
It's going to convert that text to the format that you provided so.
It's going to use format specifiers just like the printf function.
So the format can be a sequence of characters, which is called a string.
We haven't studied that in a lot of detail yet.
But again, we will when we talk about data types.
And then also you can specify other data types that we're
going to talk about such as integers, numbers basically.
So you can read numbers.
You can read strings.
You can read numbers that contain decimals, single individual
characters, all this nice stuff.
If the standard in is input from the keyboard which is default what's
going to happen is as you type in text via the keyboard, it's going to take
that text, put it into the c program and it's going to convert that text.
And text is things like letters, digits and punctuation.
So even though you're typing in a number, it's still text because
it's coming from the keyboard.
So if you enter the integer 2014, which is a number and essentially
what's happening is you're typing characters two zero one and four.
And what's happen and what will happen is it will add send
that input to the c program.
And if you want to store that data as a number, then you're going
to have to convert that data.
And so you have to specify the format specifier how you want to convert
it, just like you do on the printf.
Remember like with printf you use percent d to print an integer.
You would convert the text to an integer using percent d as
well for scanf if you wanted to convert the string to a number.
And that's what we're going to do for basics early on.
We're just going to use numbers because we know numbers so
far, and we haven't really learned about strings yet.
So we're going to mainly use numbers when we input.
So scanf.
It's very similar to printf.
But instead, it is a little different in certain arguments that it takes.
It still uses a control string followed by a list of arguments.
So there's going to be something in those double quotes.
The double quotes typically will only contain one thing and it
will be the format specifier.
How you're going to convert the data.
So the control string indicates the destination data types for
the input stream of characters.
It's that format specifier, percent d, percent c, percent s,
percent f for flow and so forth.
The printf function uses variable names, constants and expressions in
its arguments because we learn that.
You have something in double quotes.
And then inside the double quotes, you use format specifiers.
And then after the commas, you can print out variables or you
can even print out expressions.
Expressions, we'll talk about more detail as well, something
that essentially returns a value.
Most common type of expression is an arithmetic expression.
The scanf function is different because instead of actually
displaying variables, it's going to use variables that are pointed to.
But luckily pointers is a big concept in c.
You don't have to know anything about pointers in order to understand
and use the scanf function.
So don't worry about that.
All right, all you have to do is you have to remember three rules about
the scanf function in order to use it.
It's going to return the number of items that it successfully reads.
So if it reads one variable, two variable, three variables,
it will return that.
If you use scanf to read a value for one of the basic variable types
that we're going to discuss, such as the int, the float, the double,
then all you have to do is the variable name, the argument has
to be preceded with an ampersand.
You don't even need to know why that is right now but just do
it, any of the basic data types.
Ampersand is address.
And because scanf takes a pointer you have to de-reference, you
have to pass in the address.
The third thing that you have to remember for scanf.
If you use scanf to read a string which is a sequence of
characters into a character, you don't have to use the ampersand.
So basically scanf takes two arguments which I'm going
to show you an example.
The first argument is going to be how to convert the data that you type in.
And if you store that data in int, you use percent use
format specifier percent d.
If you want to store in string which we haven't learned
about yet, you use percent s.
And then the second argument is and it could be many arguments depending
on how many format specifiers you use because just like printf would be the
actual variable, the variable name.
And if the variable is everything but a string, you just use the ampersand.
Otherwise, you don't use the ampersand.
Scanf function also uses whitespace to decide how to divide the
input into separate fields.
And whitespace includes new lines, tabs and spaces.
And again, I'll go through an example to show you what that means because
just like printf can display many, many variables depending on how many
format specifiers you have inside the single quotes, same thing with scanf.
If scanf has multiple things inside the double quotes, it
will read multiple inputs.
And the inputs are going to be divided by new lines.
So again, if you want to try to understand what scanf is,
it's the opposite of printf.
Printf is going to convert integers, floating point numbers, character
strings, all these different data types and it's going to convert it to
text, so that you can see it output.
And it's going to be displayed on the screen.
Scanf is the opposite.
It's going to take as input text and convert it to any
of the different data types like ints, floats and doubles.
So let's look at an example.
This is an example of our a simple program.
We know what the main routine is.
We know what the include directive is.
We haven't learned about characters and integers yet.
But basically at the very top of that main function, we're creating two
variables because we need to store the data, and we're going to talk
about variables in much more detail.
But just like the printf function, again, you're just doing the opposite.
So here we're displaying output to the screen saying enter a value.
Scanf inside the double quotes has format specifiers
to read in the input.
And so you have to specify the data that's going to be read in,
here it says percent s, percent d.
What that means is we're reading in a string, then we're reading in an int.
And you'll notice that the string doesn't have the ampersand and scanf
after the first comma.
And after the second comma, we have an ampersand because that's an integer.
We're reading in a number.
And then the printf is displaying output.
So it's saying you entered in this string in this integer, just like
we know what printf does and then the main function returns a zero.
All right, so let's go through this example in a second in code blocks.
But let's just reiterate a couple concepts.
When a program uses scanf to gather input from the keyboard, it
waits for you to input some text.
So the program is actually going to pause until you enter the data.
And then once you enter some text.
And if you enter some whitespace, if you're entering in multiple
things, then the program proceeds and reads the input once you
actually hit the enter button.
You have to hit the enter button.
Again, scanf expects input in the exact same format that you provided.
So if you say percent s and percent e, that means you have to provide
a string followed by a space, any number of spaces followed by
an integer, a number and a word.
String is a word integer is a number.
So let's go through this example line by line in code blocks.
Okay, so here we have our main function.
And again, it's important to be able to enter data into a program.
So just like the printf, it's going to be very similar, and we don't
have to understand all this code.
And we even get rid of the parameters that are being
passed, simple main function.
We don't have to understand all this code.
We don't even have to understand what variables are at this point.
But variables will store data.
In the example, we're creating a string array of 100 and with
a semicolon and then we're also just going to create an
integer, just like in printf.
We have an integer, but we're using a string.
I just want to demonstrate different inputs.
Then usually whenever before you call the scanf function to get input
from the keyboard, you're always going to tell the user something.
You want to tell them what to enter.
So printf, remember, displays data to the screen.
We're just going to say enter a value.
And they're going to actually enter a couple and then we
end it with a semicolon.
We have to tell the user to enter something.
Now here's where we use the scanf function.
We say scanf and then inside the parentheses the first argument
inside the double quotes is going to be data that we're going to read.
So in this example, let's just say we're only going to read one
input percent d which is an int.
And then we're going to store that data in a variable
because we want to look at it.
And it's going to be I.
Now we have to specify the ampersand before that because, remember,
this little trick with pointers.
You don't have to know why, it's just the address.
So that scanf, on line 17, is going to actually read data from the keyboard
until the user hits the enter button.
And then that data is going to be stored in a variable called I.
So now we just want to print what we entered.
So here we're just going to say, we're going to add a line feed so
it's readable so we're going to use our escape sequence, backslash
n and we're going to say you.
You entered percent d and we're going to display the value of the variable.
And we're also going to put a line feed after.
So the value of the variable that we want to display is i.
We're going to end it with a semicolon.
And then that's our program.
So again printing output, asking for a value, reading an input line 17
specifying that the data that we're reading is an integer, storing it
in, i using an ampersand, and then print out what we just entered.
So let's build this and let's run it.
And you'll see the program starts and then it pauses because it
wants you to enter a value.
It says enter a value.
So at this point, I have to enter a number that I can convert to a string
because that's what's happening.
So here I'm just going to say 56.
And then, I'm going to hit enter.
It ran twice because my virus scanner.
So I'm going to say 56 and then enter.
And then it prints out, I entered 56.
So I was able to enter information, number and it stored it in a variable.
Now as just like printf, you can actually have them
enter multiple things and store in multiple variables.
So here after they end in an integer, I'm going to say another
percent, format specifier percent s, which means I want them to enter a
string, which is just a seek a word.
So here, remember, if it's a word, I don't have to provide the ampersand.
So I'm just going to say stir, I want to store that
data in the variable store.
And again, we'll learn about variables.
And then I'm going to just print it out.
And I added another variable so you know the printf and I'll just
put colons before you also have to specify the format that you're
printing, so they're percent s.
So hopefully that's clear.
I'm now going to read in two inputs separated by spaces,
store them in two variables, I and stir and then display them.
So now if we build and we run.
So here I'm going to enter my number 22, I'm going to hit space
and then I'm going to enter my name and then I'm going to hit return.
When I do that, the program is going to on pause and it's
going to display the number I entered and then the string.
So it displays 22 and Jason.
So this is how you can get input from the user, simple
four or five lines of code.
One other quick thing I want to show you is how you can read in a double.
So later on in the lessons, you're going to be shown how to read into
float and that's usually using a format specified percent f.
But if you run and read in double, it's actually a little different
you use percent lf, so kind of keep this in the back your mind.
If I create a variable name double x and then I use scanf to read that in,
I need to read it in using percent LF.
That's the format specifier you want to use.
And then again just like the other ones, you would use the
ampersand for address and say x.
And so this is how you can specifically read in a double.
And that's a different than reading in a flow.
So keep this in the back your mind.
When you go to run this, you'll notice it works pretty straightforward.
And you can read input reading a double.
So you just go ahead and run that.
And so it's going to ask for double, I didn't really do much.
And this doubles be like 77.8 and then we'll read it in and store
it, it's pretty straightforward.
So the scanf is very useful as I mentioned.
Other things with the scanf you'll notice as you go through
the course is that scanf sometimes doesn't work that well.
You can use f get s for other things.
The main issue with scanf is when you read in, you're only
reading in until you see a space.
And so if you try to use scanf, you try to call it
multiple times in a function.
With other combinations, it may not work very well because what's
happening is when you hit the return button on the keyboard,
it's going to it's going to not read that return button because
it only reads up into a word.
And so it's not going to get the line feed.
So the next time you use scanf, it's going to immediately
skip that line and it's going to read in the line feed.
So this is a problem you'll see if you write multiple scanfs in a
single program one after another, you'll notice that the other
scanfs aren't executing because they're reading in the line feed.
A very easy way to get around that is right after you do a scanf you
just call git char and then you call another scanner, but that's more
of a concept to talk about later.
This was just to show you when you read in a double using scanf,
make sure you use percent LF.
And again, this is useful because some of our challenges will align
you to get input from the user in order to do useful things and
learn different concepts, we be able to get input from the user.
So I hope this helped.
And again, don't worry about some of these concepts that you're
not fully understanding things like variables and strings,
we're going to discuss all that in a lot of detail, thank you.