Writing Our First C Program

In this tutorial, we will learn to write our first program in C.


Throughout the course, I may use code and program interchangeably. They both mean the same. 

To begin with C programming, let’s open the Code::Blocks IDE. Click on “File” in the top menu, then select “New” and choose “Empty File” from the options.

Save the file with the name hello.c and type the following code exactly as shown into this file.

#include <stdio.h>
int main()
{
    printf("Hello World!");
    return 0;
}

For now, don’t worry about understanding what this code does. We will go through each line and comprehend its purpose as we move forward.

To execute the code, follow these steps:
First, save the code. Then, navigate to the top navigation bar and click on “build.” From the options that appear, select “build and run.” Alternatively, you can press the F9 key on your keyboard. Upon doing so, you will observe the output displayed as follows:

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

Let’s concentrate on the first line for now. It indicates that the text written inside the printf function will be shown as is, without any additional brackets or quotation marks. But why is this the case?

To gain a better understanding of why we obtained this output, we must examine each line of the code we have written. Therefore, let’s take a closer look at the code, line by line, to comprehend its meaning and purpose.


Line 1:  #include <stdio.h> 

We will first understand the meaning of stdio.h in #include <stdio.h>

What is stdio.h?

stdio.h is a header file (a file with .h extension is called a header file) which contains predefined input/output functions like printf(), scanf(), getc(), putc() and so on. Here, stdio stands for standard input output. It has all the necessary functions required to perform input and output.

In general, a function is a tool that receives input, performs certain operations on it, and produces an output. I used the word “generally” because functions can sometimes operate without any input or output. It all depends on the specific function. Throughout this course, you will gain a detailed understanding of functions. For now, you can visualize a function as a machine that accepts input, processes it, and produces a corresponding output in response.

Broadly speaking, there are two types of functions:

  1. Predefined functions
  2. User-defined functions

Predefined functions are functions that are already defined for us and are readily available for use, such as printf() and scanf(). On the other hand, user-defined functions, as the name suggests, are functions that we, as programmers, define ourselves.

The stdio.h library contains a set of predefined functions that we can utilize in our code to speed up the development process. In our program, we are using a function called printf(). The code for this function has already been written by someone else, and we are simply using it in our code. Just imagine the effort required if we had to write this function from scratch. It would be quite cumbersome and time-consuming. That’s why predefined functions exist—to save us time and make our coding process more efficient.

What is #include then?

By using #include in our code, we are instructing the preprocessor to include the header file stdio.h in our code, so that we can use some or all of the predefined functions available in the header file.

What is preprocessor?

Before we delve into the concept of a preprocessor, it’s important to mention another term that you will come across frequently: the compiler. A compiler is a program designed to convert C code, which is understandable only by humans, into machine code that computers can understand. This compilation process is crucial because it enables computers to comprehend our instructions and perform the desired tasks.

Now, let’s focus on the preprocessor. Think of the preprocessor as an editor whose main role is to replace commands followed by a # symbol with the necessary content required by the program before the compilation begins. This is why it is called a preprocessor, as it prepares the code for compilation. Anything that follows a # symbol is referred to as a preprocessor directive, and it is meant solely for the preprocessor’s processing. The compiler does not involve itself with these statements.

The preprocessor performs various tasks, which we will learn about as we progress in this tutorial. In our code, the preprocessor will replace the directive #include <stdio.h> with the contents available in the file stdio.h. This ensures that the compiler receives the final file, containing all the necessary contents, for successful compilation.

So, this is all we need to understand about #include <stdio.h>. Now, let’s move on to consider the second line of the code.


Line 2:  whitespace

A whitespace is added to improve the readability of the code.


Line 3:  int main()

The main() function serves as the starting point of a program. It is a special function defined by the user where the execution of the program begins.

The int before main signifies that the main() function will return an integer value. This is known as the return type of the function. Typically, a function returns some output, but it is not always necessary. Instead of int main(), we can also use void main() to indicate that the function does not return any value. However, it is considered good practice to have main() return an integer value.


Line 4:  Opening curly brace ({)

As we are aware, main() is a function that is defined by the user. This means that it is the responsibility of the user to specify what the main() function will do. The opening curly brace ‘{‘ right after the main() function marks the beginning of the function’s definition. From that point onward, every statement written is considered part of the main() function.


Line 5:  printf(“Hello World!”)

Unlike the main() function, printf() is a function that is already defined for us. This means that its functionality has already been determined by someone, and we can use this function directly in our code. When we use printf(), we don’t need to know the inner workings of the function or what’s inside it. It’s one of many predefined functions that we can use in our programs, and they are designed to make the development process faster and easier. It’s worth noting that the printf() function is part of the stdio.h header file.

The term “printf” stands for “print formatted,” and its purpose is to display the value provided to it within parentheses on the output screen. In our case, the output screen is the black console window that you’ve seen before. This function is a clear example of how a function takes some input (the value we want to print), processes it, and produces some output (in this case, displaying the value on the screen). That’s why we see “Hello World!” as the output on the screen.

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

Kindly ignore the subsequent lines as they are automatically printed. 

Please note that Hello World! is printed without double inverted commas. They are simply ignored and whatever inside double inverted commas will only be printed.


Line 6:  return 0

The statement return 0 at the end of the main() function signifies that the main function has executed successfully. This value, 0, is then returned to the operating system. That’s why we see the message “process returned 0” printed alongside the “Hello World!” text. It’s an indication that the program ran without any errors and completed its execution smoothly.

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

You might be wondering why we specifically use 0 as the return value in the main() function. The reason is quite straightforward. In C programming, returning 0 from the main() function is a convention to indicate that the program has executed without any issues or errors. It’s a standard way of signaling successful completion. On the other hand, any value other than 0 is considered an indication of an abnormal termination of the program.

However, it’s important to note that you have the flexibility to return any value you prefer from the main() function. If you choose to return a different value, that value will be displayed on the console window or black screen when the program finishes executing. So, while 0 is commonly used to signify successful execution, you have the freedom to select a different value if desired.

For example, I have changed the return value to 90 in the original code as follows:

#include <stdio.h>

int main()
{
    printf("Hello World!");
    return 90;
}

Output:

Hello World!
Process returned 90 (0x5A)    execution time : 0.845 s
Press any key to continue.

If you entered 90 as input and the output also shows 90, it indicates that the program has executed successfully. In this case, the return value from the main() function can be any value you choose. However, it’s important to ensure that the same value is printed on the screen as the output. This way, you can verify that the program ran correctly based on the expected output. The specific return value itself doesn’t affect the program’s execution, as long as it matches what you see on the screen.


Line 7:  closing curly brace (})

The closing curly brace indicates the end of the function definition. So in the original code, printf() and return 0 are part of the main() function as both these statements are within the opening and closing curly braces.

With this we have successfully learned the different parts of our program. 


Interesting Observation

You may have noticed that Code::Blocks offers other options in the “build” menu besides “build and run”. One of these options is “run” as shown in the picture below.

When we click the “run” button in place of “build and run”, a popup appears which looks like the following 

Once you click “Yes” in the dialog box, the console window will appear. This indicates that we cannot skip the step of building the code. It is necessary to build the code before running or executing it.

Now, you might be wondering what exactly does “build” mean and why does the IDE require us to build the code before running it. We will answer this question in the next article.

Stay tuned for more information.



Leave a comment

Leave a comment

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

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.