- A character is a single letter or symbol.
Put two characters together and you have a string.
But strings are funky things in this C language.
They're not a variable type, just a clutch
or array of characters, all marching together in a line.
In this movie, I introduce you to the string.
You'll see how strings are created in C,
how they can be filled by using string input functions.
You'll also meet the scanf() function,
which is used to pull in all sorts of input.
Line five probably looks a little odd to you.
It's a variable declaration.
A char variable named password is created.
The string spatula is assigned to the password variable.
This is known as an immediate assignment,
which is possible with other variables, as well,
but necessary when declaring a string.
By the way, this is an array declaration,
which is a topic for a later movie.
The square brackets indicate
that more than one char variable is present.
How many?
The compiler figures that out,
based on the size of the string.
Otherwise, the value would appear between the brackets.
In line seven, a printf() function
displays the string's value.
The %s placeholder is used.
It's difficult to see because of the escaped double quotes
and the new line.
Build and run this code.
The output properly shows the contents of the string.
Change the word spatula in your editor
to the words fuzzy wuzzy.
Put a space in there, as spaces are
valid password characters on some systems.
Save the changes.
Build and run to see the string displayed.
Remember that some characters must be escaped in a string.
These include the double quotes, new line, tab, and so on.
I'll add them to the string here.
Double quote,
new line,
tab.
Save.
Build and run.
And there's the predictable but ugly output.
To fetch a string from standard input,
or to fetch any specific value, use the scanf() function.
As an input function, scanf() is declared
in the stdio.h header file,
which you must include in your code
lest the compiler become confused.
Scanf() uses printf()'s placeholders
to read a specific value into a variable,
and that variable is often prefixed with the ampersand.
Here's the format for the scanf() function.
It includes two arguments:
a formatted string and a variable.
The formatted string directs scanf() to look for
a specific type of value.
To accomplish this, you specify the same placeholders
used with the printf() function
inside the formatting string.
So, %d for an integer, %c for a character, and so on.
The formatting string is almost always
composed of these placeholders and nothing else.
To assign a value, specify the variable
that's already been declared in the code.
The variable type must match the placeholder.
You need to prefix an ampersand operator
to the variable name,
which is something you must remember to do.
You do not need to prefix the ampersand
to a string or array,
which is something you'll forget to do.
The ampersand used here is the memory location operator,
which is covered in a later movie.
This code demonstrates how to read an integer value
from standard input.
The scanf() function on line eight
uses the %d placeholder to fetch the integer value.
It's assigned to variable x.
See the ampersand.
Don't forget it.
Build and run this code.
Type an integer, which is any whole number.
The program assigns that value to variable x
and then displays the result.
Now, if you were to modify this code
to read in a floating point value,
you would have to make some changes.
Try to think of the changes on your own
as I make them in the code.
First, of course, would be to change
the variable type at line five to a floating point value.
Simply change int to float, and suddenly,
variable x becomes a floating point value.
In line seven, obviously,
we need to change the word integer.
Line eight, change %d to %f, for a floating point value.
And finally, at line nine, change the word integer
and change the placeholder, as well.
Save.
Build and run.
Type a real number, such as 45.6,
and the value is read and displayed.
Don't freak if you see a value that's not exactly 45.6.
What you see here is called a precision error.
Computers approximate real numbers,
which is acceptable.
In a later movie, I'll show you how to fix the output
to make it look more agreeable.
You could also modify the code so that it reads in
a single character value.
The changes are the same.
Try it along with me, if you can.
First, you change the variable type from float
to a single character variable.
And then, here, change the prompt so that it says character
and change the percent placeholder in the scanf() function
to a c from an f to read in a single character value.
Change the prompt here,
and the placeholder.
With variable x being a character variable,
you need the %c placeholders to read in the proper values.
Save the changes.
Build and run.
And I'm going to type the mysterious character x.
You can also use the scanf() function to read in strings,
but you'll find some peculiarities in this operation.
Open exercise file scanf2.
On line five, the name variable is created.
It has room to store 14 characters,
plus one additional character for the null
at the end of the string.
The scanf() function at line eight uses
the %s placeholder to read in a string.
The variable name is used without the ampersand.
That's because name is a character array,
and arrays do not require the ampersand operator.
Build and run this code.
Type your name and press Enter.
The string is read and displayed.
And now for the peculiarities.
Run the program again.
This time, assume that your name is Jerry Bob,
which is two words.
The scanf() function stops reading text
at the first white space character.
That's a space.
Tab our new line, and there's no way you can force scanf()
to read in those characters.
It's just the way it works.
So scanf() is really a string input function
where strings are just one word long.
A better function to use for reading strings is fgets,
which is a file input function.
But it can be used to read standard input.
Open the exercise file fgets1.
A variable input is created at line five.
It has room for 63 characters
plus one for the null character
terminating the end of string.
The fgets function at line eight reads standard input.
The first argument is the variable
into which input is stored.
This location is also called a buffer,
which is just nerd jargon for 'storage.'
The second argument indicates the size of the buffer.
And, again, 64 is used.
That's 63 characters plus the null character.
The final argument is stdin, which is standard input.
The puts function at line 10 reads the stored text
and sends it to standard output.
Build and run this code.
Type your instructions.
Press enter.
And there's your string, spaces and everything.