- [Instructor] Welcome back.
This week our goal is to
review the Imperative paradigm.
We're gonna be using the
programming language C.
(mumbles) it's important to remember
that the Imperative paradigm
is all about fully specified
data and instructions.
Also that the Imperative
paradigm is focused
on how rather than what,
so how we do something
instead of what we are doing.
And the meaning of that is
that we are going to be working
with variables, functions,
and controlled structures,
loops and conditions.
The good news is that most of these things
are similar in Java, C/C++, and C,
therefore, because you know Java,
you already know them.
Conditional statements and
loop statements are the same
in Java, C/C++ and C.
Functions are really similar.
We're gonna review small differences.
However variables and in
general, data handling,
is gonna have important differences,
and we are gonna use most
of our time this week
reviewing that.
Particularly we need to review arrays,
and something called structs and pointers.
Obviously there are no classes in C.
C is an Imperative language.
Classes are going to appear in C++,
and Java is going to take
that concept from there.
However you will notice that the compiler
that we are gonna use work with C
and also with C/C++,
because several concepts
apply to C and C++.
Sometimes we are gonna read C/C++
as one way to identify
that we are talking about
both languages.
So in the rest of this
video I'm gonna review
what do you need to get started with C,
then in another video I'm gonna talk about
primitive data types, arrays and strings.
Then we're gonna review pointers,
then typedef, enum and struct,
and we're gonna end it with
reviewing function calls
and parameters in C.
Let's start.
You need to install a compiler for C.
My recommendation is that you can install
Microsoft Visual Studio.
That environment is
gonna be useful for both
C, and later, the next week, for C++.
There is a document with instructions
about how to get and install
Microsoft Visual Studio,
so I recommend you to go to Blackboard
and follow instructions
to have this environment
in your computers.
As a plan B, I am sharing with you
first an online compiler for C/C++,
the link is on the screen.
You can go to this web page,
and using your browser,
you can write a program in C,
and you can compile and run
the program on the browser.
I do not recommend this one
to do big problems like
your project for this week.
However, it's useful if you want to test
small programs, a few lines of code.
Some of the examples that
I'm gonna be showing you
in the videos,
are going to be done in
this online compiler.
And finally, another option,
Dev-C++ is an all development environment.
However it's (mumbles)
it will be useful if you have issues
installing Microsoft Visual Studio,
or if you do not have
enough space, or something.
So again the online compiler
and Dev-C++ are plan B.
My advice, install Visual Studio.
We are gonna be using it this week,
with C, next week with C++,
and later one more week,
that we're gonna be practicing
both C and C++ together
before the midterm.
I'm going to provide an introduction
to the topics mentioned before.
However you need to read the textbook
in order to fully understand
and practice each of them.
Additional reference that you can use
instead of the textbook,
or with the textbook,
are the following:
number one, the best resource to learn C
is the book from Kernighan and Ritchie.
Dennis Ritchie is the
author of the language C,
so this is a good book to read.
I am showing in this slide a link
where you can find the
first edition of the book,
so you can take a look.
Additionally I am sharing
with you this tutorial,
it's a PDF file,
and covers most of the topics
that we are gonna be reviewing this week.
Programs in C are
collections of functions.
Functions are what we
call methods in Java.
Some functions are
defined by the programmer,
and there are functions in libraries,
so you can use libraries in your program.
Also we can use variables,
and there are local variables
inside of functions,
and global variables.
Global variables are variables
that we declare outside
of any function.
Finally, like in Java,
it's mandatory to have a function
with the name main,
and that is the entry
point for the program.
This is a program in C.
First, notice the comments.
The comments are the same as in Java.
You can have multiple line comments,
or single line comments.
Second, the #include,
that is equivalent to the
keyword import in Java.
That help you to include libraries.
In C the name of the library
going inside of the angular brackets,
and all the libraries are files .h.
So in this example,
stdio.h and math.h are libraries.
As you can see, the
keyword class is not here,
so no classes.
The full program, the
file, have two methods.
My function and main,
and there is one global variable, foo.
Instead of methods, we call
main and my function, functions.
So methods, when they
are inside of classes,
like in Java, or in C/C++,
but when you do not have classes,
you call them function.
Most of the lexical, syntactical,
and semantical rules from
Java apply to C and C++.
An important characteristic of main in C
is the fact that main can
be of type void or int.
If you remember in Java,
main is void,
however, in C, and in C/C++,
this can be void, or int.
Moreover, you can just
write main, without a type,
at the beginning,
and by default it's going to be int.
If you do not put a type to a method in C,
by default that means
that you want the method
to return an integer value.
Moreover, regarding
the parameters of main,
you can have main without parameters,
or you can have main with two parameters,
one integer, and the other
an array of characters.
We're gonna review arrays later.
So main without parameters,
or main with two parameters.
Both options are correct.
Therefore, you can have
seven different ways
to represent main, as you can see here,
and all of those represent the entry point
of your program.
This is quite different from Java,
because in Java, main has
one and only one signature.
Here you have different signature.
To print something on the screen,
we use the function printf.
That function is in the library stdio.h,
and it has two options.
Number one, you can print a string,
using the string as a parameter,
like here with Hello World,
but if you want to print a variable,
or if you want to print the result
of calling a method, like here,
you need two parameters for print.
The first is a string,
with any text that you want to print,
and with control symbol.
The control symbols tell the compiler
what are you going to print.
Control symbols start with
the percentage symbol,
and then a letter.
The letter d represent integers,
f floating point numbers,
c characters, and s strings,
and basically that are the types
of the variables that you're gonna put
as a second parameter.
For instance here, you want
to print the string result,
and in that position you want to print
a floating point number.
After the string, after the comma,
whatever you put here,
function, variable, or whatever,
that value is gonna be printed
in the position of the control symbol.
So when you want to print
strings and variables together,
usually in Java,
what you did was to
concatenate everything.
So you use the plus symbol
to add strings and variables,
like here.
Now in C what you need to do
is to put together the string
and the control symbol.
And later, put the names of the variables
separated by commas, like here.
Regarding the input, to read
something from the keyboard,
we use the function scanf.
This function has two parameters.
The first one is a control symbol
that represents the type of the value
that we want to read.
For instance, percentage d
to read an integer
number from the keyboard.
And the second parameter,
and this is something new,
is the address of the variable
in which we want to store that value.
This is a key idea in C.
Here in this line,
we want to store the value
that we read from the
keyboard in the variable i,
but instead of passing
this as a parameter i,
we pass as a parameter the address of i.
The ampersand is an operator in C,
and that operator returns
the address of the variable
that we put in the right side.
So ampersand i gets the
address of the variable i,
and in that address the function scanf
is going to store the number
that the function reads from the keyboard.
In this example, when the user types four,
the number four is gonna be
stored in the variable i.
Later, if you print i,
you're gonna have four
printed on the screen.
Notice that scanf needs the
address of the variable,
but printf uses as a parameter
the name of the variable, not the address.
Usually when you use scanf
the second parameter is going
to be ampersand something.
Finally, regarding the control statements,
conditional statements
and loop statements,
the ones that you know from Java,
they work in the same way in C.
So you can use for, while, do/while,
if/else, and switch, as usual.
The main difference that
you need to be aware of,
is that there are not Boolean values in C.
The key idea in C is that
zero represent false,
and any other number is
considered a true value.
Therefore in this example here,
if two plus two, because
that number is not zero,
that condition is considered true.
Therefore that program will print Hello.
And the same thing works for
the condition and the loop.
With this idea you can
start programming with C.
My advice is go open the compiler
and go ahead and write your first program.
Start with a program
with one function, main,
then create more.
Try with a control statement,
create variables, play
with print, and scanf,
and when you are ready
move on to the next video.
