What is programming? Be an expert in Programming ( part 2 )
The function is called main and the empty
parentheses indicate it doesn't take any arguments. Now, since this particular function
is designed to return an integer, we should return something.
So we'll end with a return zero. Now, all we have to do is write some code, that output something I'm going
to use the print function. Hello world from C the print function takes as an argument.
This string, hello, world from C. we need to
save this particular file. Since I've already created
a file name with dot C, all I have to do is hit file and
save we've written and saved our first program using our text editor.
Now we're going to actually execute
the program using the command line. I really like executing
programs on the command line. It makes me feel really old school like
I'm using text-based computers back in the seventies and eighties, because they
did it the exact same way. All right, let's turn to our terminal
and run this program. Executing a C program is a two-part
process. We're in the correct folder. So now we're going to compile the
file into an actual executable right now it's a text file and the SI
interpreter, can't read a text file. We've got to compile it into machine
language. So we'll do that with GCC. The file we want to run is hello or
compile rathers hello, world dot C. And we want to make an executable, which I'll call hello world.
And after a moment, it doesn't
look like anything happened. But if I use the list command there, you can see we've got a hello world dot C. And we've also got just a plain old
hello world. That's our executable. I'm going to run that by
hitting the dot the slash, and then the name of the
file. There's no dot C there. So I'm just going to run
it like that. And there, you can see the program executed.
We got the text, hello world from C. if we want to changing in
to the program, for example, add another line of output. I'm going to add a backslash in for a new
line here, and then we'll print again. My name is Mark last
off, not very creative,
But we'll do the trick. So I've got to recompile the code in
order to run this again. This way, the executable will reflect the new
line of code that I've entered on line five. Let's go ahead and use our GCC
command again to compile what we've got. And now if I run it by
running the executable, we have both lines of output
in this part of the video. We're going to learn how
to declare variables. Now I know a lot of you are coming from
Java script where you just declare a variable with the keyword VAR.
It's not quite the same in C. The reason for that is in C
we're directly allocating a specific amount of
memory with our variable. Some types of variables use very little
memory. Others will use a whole chunk.
So I'm going to show you how to declare
your variables by type and by extension allocate the correct amount
of memory for the various. So there's actually two steps to
creating a variable in C first, we have to declare the
variable via its type. So let's say our first variable we're
going to create is a charter variable or a character variable. And then
we got to give it a name. I'm just going to call this one X. Now that we've declared it, we
can assign it an initial value. Since this is a char, it's got to be
a character. So let's say X equals M. So the two step process is
again, I'll use comments here, which are ignored by the C compiler, declare the variable by type,
And then initialize the
variable with a value. So now we have that M in memory, we can also multiple
variables at the same time, so we could declare Y and Z and
then initialize those separately. So Y equals again, a single character, a and Z equals again, a single character L character variables are
one type of variable. We can declare integers are another. So let's say age, that's a much
more descriptive variable name, 47 notice. There's no quotes
around the numerical values. And I also combined my declaration
with initialization into one step. So this is combined
Declaration and initialization. You'll see that pretty frequently because
it's just one less line of code that you have to write. In
addition to integers, we have floating point numbers, a floating point number
might look like this, and then we can initialize it F indicating the float
there for a floating point. Number doubles are floating
point numbers with more precision. So I'll call it exact, and we'll say two points. I don't know, a whole bunch of values there, and that's a much more exact
floating point. My number,
If you think about it, a integer probably requires less
memory than a floating point number in most architectures, because
it's less precise at the same time, a double requires double the memory
of a regular floating point number because it has more values
and is even more precise. So these declarations
are about C providing the correct amount of memory
to store these values. It's a think of this, like moving and
moving boxes. If you've ever moved, your whole life becomes about boxes and
you want to get the right size box for the right size stuff. Well,
when you declare a variable, you're declaring a box of specific size, a certain amount of memory to
hold the values of that type.

0 Comments