>> DAVID J. MALAN: Let's implement a program that takes advantage of the
CS50 library, using a function called get int, whose purpose in life is to
do just that, to getting an int, or an integer, from the user.
Well, to use this function we first need to include the CS50 library's
header file, which we can do with the sharp include statement.
>> Let's next, as we often do, include standard io.h so that we have access
to a function like printf.
Let's now declare main, itself, with int main void, open curly brace, and
preemptively close curly brace.
And let's now proceed to prompt the user for two integers, and let's call
them, for the sake of discussion, x and y.
And let's finally add those two values, x and y, together so as to
implement the very simplest of calculators.
>> printf, please give me an int.
And now we need to actually get that int from the user.
To do this, I'm going to declare a variable called x, thereby allocating
some memory in the computer for this variable, x.
And now let me assign, using the assignment operator, the return value,
so to speak, of get int.
In other words, on the right hand side of this expression, let's call get
int, which is a function declared in CS50.h, and allow get int to do the
heavy lifting of getting an int somehow from the user, returning it,
so to speak, and then storing it from the right hand side of this expression
into the left hand side of this expression.
>> Let's next do the same thing, this time getting a value for a
variable called y.
printf, please give me another int.
int y, thereby declaring a new variable, this time called
y, equals get int.
In other words, I can call get int multiple times, and each time it's
going to return whatever integer the user has actually typed.
>> Finally, let's add x and y together.
printf, this sum of %d and %d, so in other words, I'm going to plug in the
values of x and y for those placeholder values.
%d is, well, %d.
In other words, if I add one int to another int, I'm going
to get a third int.
Therefore, I'm going to use a third place holder for an int.
>> Period, backslash n, just to put a new line on the screen so as to move the
cursor down neatly, close quote.
>> Now, printf, in this case, is going to have to take some additional
arguments, three, in fact.
Because in that first argument, I've specified three place holders.
So I'm going to separate these arguments, as always, with commas.
The first such argument is going to be x, the second such argument is going
to be y, and the third such argument is going to be, well, just an
arithmetic expression, x plus y.
I'm going to close my argument list with a parentheses, semicolon, Save my
file, and now compile this program.
>> Make adder Enter.
I'm back at my blinking prompt.
Let's now run it-- dot slash adder Enter.
Please give me an int.
Let's start with 1.
Please give me another int.
Let's go with 2.
And hopefully, the sum of 1 and 2 is 3.