In this guide, I would like you to come away with knowledge of:
We make files in the terminal with vim
.
[dasmish@silo E111] vi firstProgram.c
You could equivalently type:
[dasmish@silo E111] vim firstProgram.c
They're aliased to exactly the same thing.
main
.
Here is how we'll do it:
int main(void)
{
}
int main(void)
{
}
Jokes aside, what do the three words mean?
int
, is the data type
of the value the function returns. In this case, it is an integer. But
other valid words include (but are not limited to)
double
for a real number
or void
for nothing.
main
, is the name
of the function.
void
,
are the function arguments . void
means that no
arguments are passed to this function, but other functions do often
have arguments, and more than one. We'll cover that later when we look
more in depth into functions.
A program is a file that contains a function called
main
.
Please do whatever you need to do to remember that.
There are a few special things about the main function:
int
function. This return value is
meant to signify to the parent program whether something went wrong.
We will almost always have the function "return" 0.
int main(void)
int main(int VariableNameA, char ** variableNameB)
We will almost always use the former option until we start working with more advanced topics.
So, let's finish our program. All we need to do is make sure that our function has a return value.
int main(void)
{
return 0;
}
return
statement here is 3 spaces from the side
(this is our coding style, but if you choose 4 spaces no one is going
to get upset), and returns 0, meaning that no errors occurred during
execution (later on, you might write more complicated programs
that might return, say, 1 on an error).
This means your program is done and ready to run. Time to save your work and run the code!
Save in vim by typing (while not in insert mode)
:w
and then quit with
:q
. Or, you can save and then with with
:wq
.
Now, time to compile and run our code. The compilation step is necessary because the computer can't "run" a C file. Perhaps you've coded in languages that the computer can "run" like Python, but there's a lot going on beneath the surface there! C does not hide it from you. You have to compile your C file into something that the computer can run. Do it like this:
[dasmish@silo E111]
gcc -ansi -g -Wall -Wpedantic -o firstExecutable firstProgram.c
Each of these flags does something special for us, and I would highly recommending using all of the flags. Every time.
Okay, now our program is compiled. Don't believe me? Do an
ls
and see it for yourself.
Now let's run our program! We can run it by typing:
[dasmish@silo E111]
./firstExecutable
Great, did it run?
What's that? You didn't see anything? Well, that's exactly what you should expect, because our program doesn't do anything. Let's change that so you can see something.
Open the file again
[dasmish@silo E111] vi firstProgram.c
And write
#include <stdio.h>
int main(void)
{
printf("Hello, World!");
return 0;
}
Don't worry too much about the syntax of printf. You'll get the full lesson about it later. For now, just understand that you need it to communicate with the terminal and that in order to use it, you must have that "include" statement before main. Compile your program again
[dasmish@silo E111]
gcc -ansi -g -Wall -Wpedantic -o firstExecutable firstProgram.c
And run it again (by the way, now is a good time to mention that you can use the up arrows to cycle through old commands)
[dasmish@silo E111]
./firstExecutable
Did you see something this time? Great! Looks like you've got your first program under your belt.
E111 Homepage