Your First C Application Tutorial

In this tutorial we learn how to write our first working program in C. We also do a breakdown of our first application, so you can understand how a basic C application works.

Your first application

In the previous lesson we learned how to set up a new project in Codeblocks. All our individual source code files will be saved inside this project.

By default, when we create a new project, Codeblocks will create a file called main.c for us.

Let’s delete this file from our project.

  1. To the left of the code editor, in the Management pane, select the Projects tab.
  2. Under the Projects tab, expand LearningC, then Sources.
  3. Right-click on the file and select Remove file from project.

We could have used this file directly to write our code in but we wanted to show how to create a new file.

  1. Go to File > New > Empty File.
  2. In the popup dialog box select Yes to save the file.
  3. Name the file “main.c” and click Save.
  4. When asked if you want to overwrite the file, click Yes.
  5. In the next dialog box we can choose Yes again.

We should now have an empty file called main.c . We can confirm this in the Management pane under Projects > LearningC > Sources.

When we wanted to create a new file called main.c , we had to overwrite the one that was already there, even though we previously removed it from our project.

Codeblocks will remove the file from our project but not actually delete the file. If we want to delete a file, we have to go into the projects directory on the computer and delete it from there.

The next step is to write our first application in the code editor.

While following along with the tutorials it’s recommended that you manually type the code into the editor and run the examples. It will help you remember the lesson better, as well as get you used to the syntax through repitition.

A C program consists of the following parts:

  • Preprocessor directives
  • Comments
  • Functions
  • Data (Variables, Constants, Arrays etc.)
  • Statements & Expressions

So let’s start at the top, and don’t worry if you don’t fully understand everything, it will become clearer throughout the tutorial series.

Preprocessor Directive

In the code editor type the following.

Line 1:
 #include <stdio.h>

This is a preprocessor directive, it tells the C compiler to include another file called stdio.h before actually compiling anything.

stdio is the Standard Input & Output library, which is a file similar to the one we’re writing that contains functions for us to use like printf, so we don’t have to write them ourselves.

The compiler will basically copy and paste the code from that file into ours.

Comments

Next, we’ll add a comment on the third line.

Example:
#include <stdio.h>

// My first C program

A comment is a way for a programmer to document a program and to enhance its readability.

A comment is basically a note alongside your code and is completely ignored by the compiler.

Function

Next, we’ll add the code for the main() function on line 4. Note that there is a left and right curly brace on lines 5 and 7 respectively.

Example:
#include <stdio.h>

// My first C program
int main()
{

}

A function is multiple code statements combined into a group to allow code to be reusable. Everything between the two curly braces is part of that group and is called a block.

The main() function is required in any executable C program and acts as a starting point for our code.

Data containers

Between the two curly braces, on line 6, we press the Tab key to indent once and add the following code.

Example:
#include <stdio.h>

// My first C program
int main()
{
    char message[] = "Hello World";
}

The code we added is a variable. A variable is one of the simple temporary data containers that hold the program’s data while it’s running.

In this case we store an array of characters to form the words “Hello World”.

Function call

Next, we’ll add another function on line 8. This function looks a bit different from the main() function we wrote earlier.

Example:
#include <stdio.h>

// My first C program
int main()
{
    char message[] = "Hello World";

	printf(message);
}

Because we imported the stdio.h file at the top of the document, we have access to more functions. One of these functions is called printf(), and it’s responsibility is to print text.

We already have some text set up in our data container so we can just tell the function to print the words that are stored in the message container. We do this by writing the name of the variable between the parentheses.

Return

Finally, we add the following, still between the parentheses of the main() function.

Example:
#include <stdio.h>

// My first C program
int main()
{
    char message[] = "Hello World";

	printf(message);

	return 0;
}

At the end of the main() function we tell the compiler to stop running the main() function by returning. We also return the numerical value 0 but that’s not important at the moment.

All of these are called code statements. They allow us to express our intentions through code, telling the program what we want it to do.

That’s it for writing our program, the next step is to compile and run it.

How to compile and run your first application

We can compile and run the program by going to Build > Build & run or by pressing the green and yellow button in the icon bar that looks like a cog and play button.

If a dialog box pops up with the message “It seems that this project has not been built yet. Do you want to build it now?”. Choose Yes.

Once the compiler has finished compiling the app, it will open a console window with the following output.

Output:
Hello World
Process returned 0 (0x0) execution time : 3.671 s
Press any key to continue.

The text “Hello World” on line one is the text inside the message variable that we told the compiler to printf(). You can press any key to terminate the application and close the console window.

Congratulations! You’ve just written, compiled and ran your first C application. Granted, it’s not very exciting, but its a great way to get started with programming in C.