Course Introduction to Programming

First Program

Course's Page 13 min Text by
User Image
Tiago Barros Pires

In today's lesson, we will finally get to the practical part and create our first program using VSCode, which we installed in the last lesson.

⚠️ This lesson is special, as we are updating the course! Previously, the CodeBlocks program was used. You can check the instructions for using it in the video and at the end of the lesson.

Using Visual Studio Code (VSCode)

File Creation

To start, we need to create a new file. First, create a folder to store your codes and then open it in VSCode by clicking on "File" and then Open Folder:

Figure 1

Select the desired folder from your computer and click on "Select Folder".

If it asks whether you trust the files, click on "Yes".

Next, click on "New File" and type helloworld.c:

Figure 2

The filename ends with the .c extension. This is very important, as it signals to the computer that the file in question contains code written in the C language.

Basic Structure and Compilation

In short, a basic C program consists of two parts: the inclusion of libraries and the main function.

Including Libraries

Use the #include <> command to include them, passing the name of the module in question between the angle brackets. See:

#include <stdio.h>

These libraries are nothing more than a set of pre-written commands, codes, and functions that can be used by the programmer to facilitate and optimize their workflow.

Each library includes specific codes for a particular task. The one we used above has a name that stands for Standard Input-Output and we will use it whenever we need an interaction between the computer and the user.

Main Function

Called int main(), this function indicates where the program will start and which commands will be executed. These commands will be placed between the braces of main(), and you will understand this structure better in the future. For now, focus on understanding and practicing. See:

#include <stdio.h> // Includes the library 

int main(){ // Declares the main function
    // Code to be executed by the main function
}

We can now check if our code is indeed a C code. So, let's run it. To do this, click on "Run Code" at the top of the screen:

Figure 3

This button is a shortcut to compile and run the C file. The process is done all at once when you click it.

A terminal will open, and the necessary commands will be executed. Notice that the program in question does nothing.

Let's now cause a compilation error to get an idea of how they are displayed. Let's change the name of the main() function to Main(), since, as we've seen, the C language is case sensitive. Note:

#include <stdio.h>

int Main(){

}

When we try to compile, the compiler cannot find the main() function, and we receive the error message:

Figure 4

First Commands

The printf() Command

Let's now look at the basic commands for creating our first program. First, the printf() function, used to print data to the screen. You should pass what you want to print inside the parentheses and between double quotes. See the example:

#include <stdio.h>

int main(){
    printf("Ola Neps Academy");
}

After we press compile and run, the program above prints the phrase Ola Neps Academy to the console (the black window we saw during execution).

Figure 5

Note that a semicolon (***;***) was placed at the end. Pay attention to this, as it is this ; that marks the end of the command and must always be placed at the end of each command.

Line Break

Look at the code below:

#include <stdio.h>

int main(){
    printf("Ola Neps Academy"); 
    printf("Ola Neps Academy"); 
}

When compiling and running the program, we notice that even though we have placed two printf() commands on different lines, the actual output will be Ola Neps AcademyOla Neps Academy, on the same line. You should, therefore, use the special character \n where you want to make a line break. Notice:

#include <stdio.h>

int main(){
    printf("Ola Neps Academy\n"); 
    printf("Ola Neps Academy\n"); 
}

Note that, now, the output will be:

Ola Neps Academy
Ola Neps Academy

Notice that it is also possible to create a line break within the same printf(), simply by placing the character in the middle of the phrase. Observe:

#include <stdio.h>

int main(){
    printf("Ola\nNeps Academy\n"); 
    printf("Ola Neps Academy\n"); 
}

The output will be:

Ola 
Neps Academy
Ola Neps Academy

For now, we'll stop here. We've learned about file creation, the basic structure of a C program, and the first command, printf(). It is worth emphasizing again the importance of remembering to add semicolons, as a large part of compilation errors stem from their absence.

See you next time! Happy studying!


Using Code Blocks

This section belongs to the old version of the course. If you do not want to use VSCode, follow the steps below.

To create a file, we have two options:

First way (simpler):

In the top left corner of the screen, go to File >> New >> Empty file (or simply Ctrl + Shift + N) to create the file.

Figure 6

Go to File >> Save file (or simply Ctrl + S) to open the window to save the file.

Figure 7

Choose a name of your preference and end it with the .c extension. This is very important, as it signals to the computer that the file in question contains code written in the C language. Press Save to save.

Figure 8

Second way:

In the top left corner of the screen, go to File >> New >> File....

Figure 9

Click on C/C++ Source and then on Go.

Figure 10

Check the box next to Skip this page next time and then click Next (only the first time you create a file, to prevent this window from showing again).

Choose the language in question (in our case, C) and click Next.

Figure 11

Press the button with the ellipsis (...).

Figure 12

Go to the location where you want to save the file and write a name of your preference next to File name:. Now, there is no need to specify the extension because this has already been done.

Figure 13

Click Finish to finalize.

Figure 14

Main Function

For the first code, use:

#include <stdio.h> // Includes the library 

int main(){ // Declares the main function
    // Code to be executed by the main function
}

To check if our code is indeed a C code, let's compile it: to do this, in Code::Blocks, click on the gear icon at the top of the screen. If there are any errors, the compiler will point them out, and Code::Blocks will show them in the Build Messages tab.

Figure 15

The program in question does nothing. You can verify this by running it using the button with a green arrow next to the gear icon we just used to compile.

To cause a compilation error, let's change the name of the main() function to Main():

#include <stdio.h>

int Main(){

}

When we try to compile, the compiler cannot find the main() function, and we receive the error message:

Figure 16

Warning about Compilation and Execution

Let's say we compile and run the code below:

#include <stdio.h>

int main(){
    printf("Ola Neps Academy");
}

If we alter the code a bit, modifying what will be printed:

#include <stdio.h>

int main(){
    printf("Ola de novo Neps Academy"); 
}

If you just press run, notice that the Ola Neps Academy from the previous program will be printed again. This happens because for every change in the code, we need to perform the compilation again.