A First Program in C

Let's begin our study of the C language by writing a simple program.

In this lesson, we will write, analyze, and compile a simple C program that prints the message Hello, World! to the screen.

A First Simple Program

It is a tradition in computer science manuals that the first program written in a new language prints the message "Hello, World!" to the screen. This tradition was started by Brian Kernighan and Dennis Ritchie, the creators of the C language, when they wrote the first manual: The C Programming Language.

Let's continue this tradition and create a program that prints the message Hello, World! to the screen every time it runs:

1
2
3
4
5
6
#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Unlike other programming languages, C does not require much boilerplate code. This term refers to the code that must be written to initialize a program but is not directly related to the logic of the program itself. In this case, the code required to print the message is minimal.

Let's briefly observe, without going into too much detail, the lines that make up the program.

The first line, #include <stdio.h>, is necessary to include the C standard input/output library. In programming, a library is a set of functions that can be used in a program. The idea is that a library provides reusable functions for common tasks. In this case, the stdio.h library includes functions for managing input and output from the keyboard.

Next, the program enters the main, which is the main body of the program. The main function encloses the body of the program within curly braces {}.

Inside, we find two statements:

  • printf("Hello, World!\n"); is a statement that prints the message Hello, World! to the screen followed by a newline. printf stands for print function;
  • return 0; is a statement that ends the program and returns the value 0. This value is an exit code that tells the operating system that the program terminated successfully.

Even though it might seem complex at first glance, in this guide we will study each part in detail.

Compilation and Linking

Now let's see how to compile and run the program we just saw.

The first step is to write the source code, that is, a text file that contains the program instructions.

To do this, we can use any text editor and create a file called, for example, hello.c. The filename is up to the user, but the .c extension is important because it indicates to the compiler that the file contains C source code. Many compilers require this extension to be present.

Definition

C Source File

A C source file is a text file that contains the program's instructions. The source file usually has a .c extension and must be compiled to be executed.

filename.c

Now that we have a file named hello.c, we need to convert it into a form that the processor can understand and execute. Source code is written in C, a language understandable by humans but not by computers.

Computers can only execute machine code—that is, a sequence of binary instructions.

The conversion from source code to machine code usually involves three steps:

  • Preprocessing: in this phase, the source file is passed to a preprocessor that handles all commands starting with a hash symbol #. The preprocessor, which we will study in future lessons, acts much like a text editor by modifying and replacing portions of code;

  • Compilation: the preprocessed file is passed to the compiler, which translates the source code into machine code, also known as object code. However, the program is still not ready to run;

  • Linking: in the final phase, another program called a linker combines the object code with additional code needed to produce an executable program. This additional code includes all the library functions (like printf) used by the program.

Fortunately, all these steps are automated. So, unless you have special needs, there's no need to perform them manually. In most cases, the preprocessor is integrated into the compiler, and the linker is automatically called by the compiler.

Compilation commands vary depending on the compiler. For now, as an example, let's see how to compile the program with gcc, which is the default compiler on many Unix and Linux systems.

Assuming we have our program hello.c and want to compile it, we open a terminal and run the following command:

gcc hello.c -o hello

This command tells the gcc compiler to compile the file hello.c and produce an executable named hello. The -o option stands for output and allows us to specify the name of the executable file.

If the command is executed correctly, we should see a file named hello appear in the current directory. This file is the executable of our program. To run it, we can type:

./hello

Which will produce the output Hello, World!.

Definition

Compiling a C Source File with GCC

To compile a C source file using the gcc compiler, you can use the following command:

gcc filename.c -o executable_name

Where:

  • filename.c is the name of the C source file;
  • executable_name is the name of the output executable file.

Integrated Development Environments

So far, we have assumed the source code was written with a basic text editor like nano, vim, or notepad.

There is an alternative: using Integrated Development Environments or IDEs. An IDE is software that includes a text editor, a compiler, a debugger, and other useful tools for software development.

The components of an IDE are designed to work together seamlessly. For example, when the compiler finds an error in the code, the IDE highlights the corresponding line and provides suggestions for fixing it.

Examples of popular IDEs include:

  • Visual Studio: an IDE developed by Microsoft for programming in C, C++, C#, Visual Basic, and other languages;
  • Eclipse: an open-source IDE developed in Java and used for programming in various languages including Java, C, C++, Python, and others;
  • Code::Blocks: an open-source IDE developed in C++ used for programming in C, C++, and Fortran;
  • Clion: an IDE developed by JetBrains for programming in C, C++, Python, and other languages.

Of course, the choice of an IDE is very personal and depends on the programmer's needs. Some prefer lightweight and minimalist IDEs like Code::Blocks, while others prefer feature-rich and complex ones like Visual Studio. Since they differ significantly, reviewing them in depth here wouldn't be very useful.

In Summary

In this introductory lesson, we learned how to write a first simple program in the C language that prints a message to the screen.

Specifically, we learned that a C program must be written as plain text in a source file, a file with a .c extension. To create such a file, we can use any text editor like nano, vim, or notepad, or an Integrated Development Environment (IDE).

Finally, we saw that a C program must be compiled in order to run. That means it must be converted into an executable file, or executable program, that the processor can understand. To do this, we use a compiler, which translates the source code into machine code.

Now that we understand these concepts, we can move on to the next lesson, where we will analyze the general anatomy of a program written in C.