C Programming Tutorial 5 – How a C Program Works – Part 2

Welcome back! Did you read the last blog? We started talking about how a C program works! Read that before reading this one. Are you new here? Start from the beginning of the series!

In this blog, we will be dissecting the hello world program that we wrote in an earlier blog!

Breaking Down the Main Function

The first thing we will look at is the main function. It is important to realize that the definition of every function looks similar to this. We have the return data type, the name or identifier, parenthesis for any arguments, and then a code block designated by curly braces.

Within the code block, we have what are known as statements.  This is when we are telling the computer to do something. We always, always, always use semicolons! It will be clearer soon when you are supposed to use a semicolon and when you are not.

What you need to do is look at this program and try to memorize all of the syntax here. If you can write this all without looking at anything, you’re doing very good.

Everything inside of the curly braces belongs to the main function. You can see that this function does not require that we pass anything into it  At the end of the function we have return 0. This is how we say that the program worked. 0 = good.

We can use this to denote whether the program worked or not. For example, we could use a -1 to say “bro something went wrong.” 0 is an int, which is short for integer. That is why the word int comes before the name of the function.

Inside of the function we are calling another function called printf(). This shows that functions can call functions. This function is used to output something on the screen. The thing inside of quotes is the argument. We give this function some data and it will spew it on the screen. Now, this \n is known as the newline character. This will make the program go down to the new line.

Includes

Now I have a question for you! How does the C compiler know what this printf() is? We haven’t defined it anywhere! Well, that brings up the concept of includes.

C actually has a bunch of functions that are already available to us and we can include certain files to gain access to new functions. This means that the printf function comes from stdio.h. Cool right?!

Conclusion

In the next blog, I’m introducing UNIX and Linux and some of the most important commands! Check it out!

Did you know that this blog series is also a video series?! Check out the video that goes with this blog, below!

Leave a Reply

Your email address will not be published. Required fields are marked *