text
stringlengths
1
81
start
float64
0
10.1k
duration
float64
0
24.9
And I'm going to create a new file by clicking this little plus,
1,781.77
2.98
and clicking New File.
1,784.75
1.22
>> I'm going to save this next one as, let's say, string.c,
1,785.97
3.28
because I want to play with strings.
1,789.25
1.5
And string in C is just a sequence of characters.
1,790.75
3.24
So now let's go ahead and do the following.
1,793.99
2.1
>> Include standard IO.h-- and it turns out standard IO,
1,796.09
5.114
IO just means input and output.
1,801.204
2.156
So it turns out that this line here is what
1,803.36
2.56
is the neighboring us to use printf.
1,805.92
2.22
Printf, of course, produces output.
1,808.14
2.27
So in order to use printf, it turns out you have to have this line of code
1,810.41
4.59
at the top of your file.
1,815
1.04
>> And we'll come back to what that really means before long.
1,816.04
2.416
It turns out that in any C program I write,
1,818.456
1.944
I've got to start it with code that looks like this.
1,820.4
3.24
And you'll notice CS50 IDE, and other integrated development
1,823.64
3.22
environments like it, are going to try as best
1,826.86
3.19
they can to finish your thought.
1,830.05
1.73
In fact, a moment ago if I undo what I just did, I hit Enter.
1,831.78
4.15
>> I then hit open curly brace, hit Enter again.
1,835.93
3.23
And it finished my thought.
1,839.16
1.27
It gave me a new line, indented no less for nice stylistic reasons we'll see.
1,840.43
4.71
And then it automatically gave me that curly brace to finish my thought.
1,845.14
3.419
Now, it doesn't always guess what you want to do.
1,848.559
2.041
But in large part, it does save you some keystrokes.
1,850.6
3.02
So a moment ago, we ran this program-- hello, world, and then compiled it,
1,853.62
5.94
and then ran it.
1,859.56
0.9
But there's no dynamism here.
1,860.46
1.407
What if we wanted to do something different?
1,861.867
1.833
Well, what if I wanted to actually get a string from the user?
1,863.7
3.93
I'm going to use a puzzle piece called exactly that-- get string.
1,867.63
3.62
>> Turns out in C that when you don't want to provide input to a puzzle piece,
1,871.25
4.61
or more properly to a function, you literally just do open parenthesis,
1,875.86
3.5
close parenthesis.
1,879.36
1.07
So it's as though there's no white box to type into.
1,880.43
5.11
The say block before had a little white box.
1,885.54
2.18
We don't have that white box now.
1,887.72
1.94
>> But when I call get string, I want to put the result somewhere.
1,889.66
3.65
So a very common paradigm in C is to call a function, like get string here,
1,893.31
4.37
and then store its return value.
1,897.68
3.39
It's the result of its effort in something.
1,901.07
3.38
>> And what is the construct in programming,
1,904.45
3.18
whether in Scratch or now C, that we can use to actually store something?
1,907.63
5.82
Called it a variable, right?
1,913.45
2.54
And in Scratch, we don't really care what was going in variables.
1,915.99
4.33
>> But in this case, we actually do.
1,920.32
1.85
I'm going to say string.
1,922.17
1.549
And then I could call this anything I want.
1,923.719
1.791
I'm going to call it name, gets get string.
1,925.51
2.83
>> And now even if you're a little new to this,
1,928.34
1.91
notice that I'm lacking some detail.
1,930.25
1.734
I'm forgetting a semi-colon.
1,931.984
1.166
I need to finish this thought.
1,933.15
1.25
So I'm going to move my cursor, and hit semi-colon there.
1,934.4
3.08
And what have I just done?
1,937.48
1.65
In this line of code, number 5 at the moment,
1,939.13
2.31
I'm calling get string with no inputs.
1,941.44
2.359
So there's no little white box like the Save block has.
1,943.799
2.291
>> I'm just saying, hey, computer, get me a string.
1,946.09
2.5
The equal sign is not really an equal sign, per se.
1,948.59
2.8
It's the assignment operator, which means,
1,951.39
2.4
hey, computer, move the value from the right over to the left.
1,953.79
4.07
And in the left, I have the following.
1,957.86
2.62
>> Hey, computer, give me a string-- a sequence of characters.
1,960.48
3.1
And call that string Name.
1,963.58
2.057
And I don't even have to call it Name.
1,965.637
1.583
>> I could call it, conventionally, something like S,
1,967.22
2.75
much like we used i to call the variable i.
1,969.97
2.93
But now I need to do something with it.
1,972.9
1.929
It would be pretty stupid to try compiling this code, running
1,974.829
2.541
this program, even though I'm getting a string,
1,977.37
2.04
because it's still just going to say hello world.
1,979.41
2.17
>> But what if I do want to change this.
1,981.58
4.56
Why don't I do this?
1,986.14
1.8
Percent s, comma s.
1,987.94
3.692
And this is a little cryptic still.
1,991.632
1.458
>> So let me make my variables more clear.
1,993.09
2.47
Let me name this variable Name.
1,995.56
1.95
And let's see if we can't tease apart what's happening here.
1,997.51
2.72
>> So on line five, I'm getting a string.
2,000.23
2.54
And I'm storing that string, whatever the user has typed in
2,002.77
2.85
at his or her keyboard, in a variable called Name.
2,005.62
2.81
And it turns out that printf doesn't just
2,008.43
2.16
take one argument in double quotes, one input in double quotes.
2,010.59
3.63
>> It can take two, or three, or more, such that the second, or third, or fourth,
2,014.22
4.88
are all the names of variables, or specifically values,
2,019.1
3.22
that you want to plug into, dynamically, that string in quotes.
2,022.32
6.29
In other words, what would be wrong with this?
2,028.61
3.5
If I just said hello name, backslash n, saved my file, compiled my code,
2,032.11
5.81
and ran this, what would happen?
2,037.92
3.74
>> It's just going to say, hello name, literally N-A-M-E,
2,041.66
3.479
which is kind of stupid because it's no different from world.
2,045.139
2.761
So anything in quotes is what literally gets printed.
2,047.9
2.5
So if I want to have a placeholder there,
2,050.4
2.12
I actually need to use some special syntax.
2,052.52
1.902
And it turns out if you read the documentation for the printf function,
2,054.422
2.958
it will tell you that if you use percent s,
2,057.38
3.94
you can substitute a value as follows.
2,061.32
2.6
>> After a comma after that double quote, you simply
2,063.92
3.27