7 minute read

If you ask a programmer which programming language you should learn as your first, they’ll often prescribe you their personal favorite, often not considering if the language is supported on multiple platforms, easy to learn, exposing underlying system mechanics, and fully featured.

This post is the first of a series of tutorials intended to teach you the C programming language, an excellent first language because of the following reasons:

  • Supported on all major operating systems
  • Relatively straight-forward syntax
  • Mature and stable feature set
  • Used in many articles, books, and other publications
  • Easy transition into higher-level languages

If you wish to use C as a gateway language, C++ and Objective-C are supersets of C. This means that they implement all of C’s features and extended it with more of their own. Several other languages such as Java, C#, D, and others are based on C’s syntax, so any of these are easy to pick up after you learn C.

What you need in terms of software for this series of articles is minor. In fact, you could get by with just a simple text editor and a compiler.

A compiler is a computer program that converts the human-readable text of a programming language into an executable program for your computer.

On the popular platforms, you can get a compiler for free, the most popular ones being GCC for Linux, Visual C++ for Windows, and Xcode for the Macintosh. While Visual C++ and Xcode contain fully-featured development environments, including specialized text editors, GCC is only a compiler. If you’d like a full-featured development environment for Linux, take a look at the Eclipse project. If you’re on another platform, please refer to the Wikipedia page listing C/C++ compilers for a suitable compiler for your platform.

Since this series of tutorials aims at cross-platform compatibility, please refer to the compiler’s documentation for operating instructions.

Now that that’s all out of the way, let’s get started programming!

Your First Program

The first program that many programming books teach you is the famous “Hello, World!” program, and this series of tutorials will carry on that tradition. Without explaining any of the concepts in advance, let’s take a look at the code required to write “Hello, World!” onto your computer screen:

#include <stdlib.h>
#include <stdio.h>
int main(void)
    printf("Hello, World!\n");
    return EXIT_SUCCESS;

Save this code into the file tutorial.1.1.c, compile the program, and run it. Your Command Line Interface should now say Hello, World!

Let’s examine this piece of code line-by-line to get an understanding of what’s actually happening in the program.


#include <stdlib.h>

This line includes the stdlib.h file into our program, meaning that the compiler looks for the file stdlib.h, and copies its contents into our program’s code at the position of the include call.

So, in short: an include makes the contents of the specified file part of the calling source code.

There are two forms of includes, namely the quoted form and the angled bracket form. They are named after the characters that encapsulate the filename, quotes, and angled brackets. Let’s take a look at both of these forms:

#include "quoted_form.h"
#include <angled_bracket_form.h>

The angled bracket form is to include files in the compiler’s path and any additional paths that you may have specified for includes.

The quoted form is generally used to include files in the same directory as your source file.

So generally speaking: use the angled bracketed form to include global files, and use the quoted form to include local files. We’ll use the quoted form in later tutorials.

The include call is prefixed with the number sign (#), which signifies that this line is a pre-processor directive. The pre-processor is the part of the compiler that executes all of these directives before the source code is compiled, and all of them begin with the number sign. We’ll cover other pre-processor directives as we encounter them later.

Header Files vs. Source Files

By now, you will probably have noticed that the file we saved ends with the file extension .C, while the included files end with the extension .H. The files themselves are plain-text files, but their intended functions differ greatly.

Files with the extension .C are referred to as source files. These files contain the meat and bones of your program, such as function definitions.

Files with the extension .H are referred to as header files. These files contain the declarations of functions and structures, not the actual code contained in them.

The difference between definition and declaration will be described in a later chapter when we’ll actually create more complex programs.

Header files are usually employed by libraries to tell other programs about functionality they contain. In our case, we’re importing code from the stdlib.h file, which is the header file for the Standard C Library. We’re including this header in our program since we’re using its EXIT_SUCCESS constant.

After we include the stdlib.h, we also include stdio.h with the following include:

#include <stdio.h>

This file declares input and output functionality and is therefor called the Standard Input/Output header. We’re including this header in our program since we’re using its printf function.

Data Types

Data types in C are used to define variables, parameters, and function return types. Here is a quick listing of the core ones that we’ll be dealing with the most:

  • char – A single character, usually* represented between two single quotes: 'd'
  • float – A decimal number, usually* represented as a decimal number, followed by an f: 1.0f
  • enum – An enumeration, we’ll explore this type in future tutorials
  • int – An integer number, usually* represented as an integer number: 567
  • void – Absence of data

We’ll explore data types as they come up in our code requirements.

* Can also be represented in a more complex hexadecimal (base-16) notation. Functions

The next line doens’t start with a number sign, and instead starts our main function definition.

int main(void)

As mentioned before, the contents of a C function are encapsulated by curly brackets. Atop of these brackets is the function prototype, which tells us what the function returns and what arguments it expects. A C function can be visualized like this:

return_type function_name(arguments)
    internal functionality

A function can return and consume any data type supported by C. For example, the following function, DoNothing, doesn’t return anything and doesn’t take in any arguments:

void DoNothing(void)

The void in the function’s arguments list is optional, but provides an additional visual aid. The following function returns an integer and also consumes two integer arguments:

int Add(int A, int B)
    return A + B;

Note that this function uses the return keyword to pass the sum of arguments A and B back to the caller. This return value can be assigned to a variable of the same type, or passed to yet another function.

To call the function DoNothing, you would put the following line in your code:


Here we see the name of the function, followed by two empty brackets (since there are no arguments to be passed to the function), followed by the line terminator. All commands that live inside of a code block must be terminated with the semi-colon.

The Main Function

The main function in our program is a special function of the C programming language. It may only exist once in your code and is called by the operating system when your program is executed. This means that any code you write in your program must in some way or another be traceable back to the main function.

Our main function contains one function call and one return statement. The function call, printf, is defined in the stdio.h header file and part of the Standard C Library. This function writes a piece of text called a string to the screen:

printf("Hello, World!\n");

As you can see, our "Hello, World!" text is included in this string, as well as a strange sequence of characters, namely: "\n". This is one of the special string characters of the C programming language, which in this case represents a new line character. Similar to pressing the Enter key in a text editor, the new line character moves the cursor to the next line of the output.

There are several special characters available for you to use, which we’ll explore when we start programming with strings.

The next line returns a value from our main function back to the operating system, in our case EXIT_SUCCESS, which signifies successful termination of the program without any errors. If we did encounter unexpected errors, and the program were to terminate wrongly, we’d return EXIT_FAILURE.


After this statement, the program terminates and the user is returned to the operating system.


That’s it!

You now have a fully functional “Hello, World!” program and know what each of its lines does. Of course, this is just the tip of the iceberg when it comes to the C programming language, and possibly one of the easiest programs to write.

I hope you’re looking forward to learning more about C in upcoming tutorials where we’ll move into the more advanced topics.