>> SPEAKER 1: All right.
That last program was a little boring.
Let's go ahead and personalize this a little bit.
Rather than to say hello world, why don't we go ahead
and say, hello David?
Well, to do this, I could approach this in a couple of ways.
I could, of course, just hard code David, but that's not all that
fundamentally different from the last version.
So let's take a leap forward, undo that change, and instead insert a new
line of code where line five once was.
>> Specifically, what I'm going to do here is add in string name, thereby
declaring a variable.
Notice that in C, not only do you have to give a variable a name, which in
this case is name, you also have to specify what type it is.
We'll see other types before long, among them integers and floating point
values and more, but for now, string is just a word or a phrase, more
formally, a sequence of zero or more characters.
So at the moment, this line five is saying, give me a variable called name
whose data type is string.
>> Now, I want to store a value in there, and to do that in C, we use the equal
sign, otherwise known as the assignment operator.
So I'm going to proceed as follows.
Equals quote, unquote, D-A-V-I-D, closed quote, semicolon.
The net effect in line five now is to declare a string, D-A-V-I-D, storing
it from the right hand side of this expression into the variable on the
left hand side of this expression.
So by the end of this line, we have D-A-V-I-D stored inside of a variable
called name.
>> Now let's use this variable.
On line six, I'm going to replace world with a placeholder.
%S is defined according to the documentation for printf as being a
placeholder for a string.
In other words, it's a way of informing printf that I'm going to put
some other value here, but let me tell you in just a moment what
it's going to be.
Now, how do I proceed to tell printf what that value is going to be?
>> Well, I need to provide printf with a second argument, and to do that, I
need to put a comma after the first such argument, which was this quoted
string, and then I need to specify what value I want to plug into that
first argument's placeholder.
So the name of my variable is, of course, name, so it suffices to type
"name" as the second argument to printf.
Now don't be misled.
Even though there is a comma inside of the quoted string, that is indeed
inside of the string so it does not separate one argument from another.
Only this comma that's outside of that quoted string actually separates
printf's first argument from its second argument.
>> Let's now compile this program.
Make hello one.
Oh my goodness, five errors generated, and my program's
only seven lines long.
Well, as always, don't look at the last of the errors on your screen.
Look at the very first because perhaps there is a cascading effect whereby
just one error up top created the appearance of more errors than there
actually are.
>> So let me scroll up, and the first such boldfaced error here is use of
undeclared identifier string.
Did you mean standard in?
Well no, I didn't.
I did mean string, but it turns out I forgot that string doesn't exist as a
data type technically in C. Conceptually it exists, but the word
"string" only exists because the CS50 staff have declared it, so to speak,
in a file that we ourselves wrote.
In fact, just as someone long ago declared the printf function in a file
called Standard IO.H, so did we the staff declare string as a data type in
a file that, no surprise, is called CS50.H.
>> So let's go back to the top of my program and inform the compiler that
not only do I want to use the symbol string, I also want to educate the
compiler on what that symbol means.
And to do that quite simply, I can go back up to line one, insert a new line
here, and add, "include CS50.H," also between angled brackets.
This instruction, much like that now on line two, is going to inform the
compiler that it should include the contents of CS50.H wherever they are
in my hard drive inside of my program, thereby educating the compiler as to
what is meant by string.
>> Let's recompile my program after saving the changes.
Make hello one.
Enter.
Much better.
Now I have a blinking prompt at which I can type, "./ hello one," and voila.
Hello David.