Your First Program

If you have already setup your environment for C development, you can start writing your first program.

Example Program

#include <stdio.h>

int main()
{
    puts("My first program");
    return 0;
}

Copy it into your IDE, compile it, and run it. You will see the following output:

My first program

But in order to actually learn something, we need to take a closer look at this program.

An Introduction to Header Files

The first line of this program looks like it begins with some sort of hashtag.

#include <stdio.h>

Basically, “include” tells your compiler that there is information in another file that your program would like to access. These files we include are called header files, and they are especially useful in large projects. In this case, we are including stdio.h, which is a header file that let’s you do things like read and write from files, print text to the console, and get user input from the keyboard. The name stdio is short for “Standard Input and Output Library”, and it is available on virtually all platforms.

The Main Function

The next part of your program is the main function, which is the entry point of your program.

int main()

The entry point is the first instruction that your program executes. As long as you are using the C programming language, your program will always start with int main. But simply writing int main is not enough, we need to tell it what to do:

{
    puts("My first program");
    return 0;
}

If you have ever used Java, C#, or some other language with similar syntax, you might recognize the use of { and }. Basically the purpose of braces is to tell the compiler where the function begins and ends. Any line of code after the left brace and before the right brace will be executed when the main function is called. The last notable part of this snippet is puts, which is the function we use to tell the program to print something out to the console. In this instance, we are printing a string, which is just a series of characters encapsulated by quotes.

<< prev | next >>

4 2 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x