Getting Started with C

In order to get started with creating programs in C, software is needed to facilitate the writing and compiling of a program. Writing a program in C can be done in a simple text editor, like Notepad, however, using an IDE, or Integrated Development Environment, can have many benefits. An IDE not only provides a means to write a program, it may also include features such as code completion, where it offers suggestions to complete a line of code, based on what has been typed so far. This is particularly useful if you are new to a programming language or, starting programming for the first time.

In order for a program in C to run, it must first be compiled. This is where a program is converted into a form that a computer can read. Some IDEs incorporate a compiler so that one does not have to be installed separately. Code::Blocks is an example of an IDE that provides a means to write, compile and run a program written in the C programming language.

To aid in the writing of a program, many IDEs, including Code::Blocks, colour code various aspects of the program to make it more readable, as well as easier to maintain and troubleshoot. Below is an example of a basic C program that displays the message ‘Hello world!’ out to the console.

#include <stdio.h>
#include <stdlib.h>

/* Basic program to display a message
   in the console. */
int main()
{
    // Display a message in the console.
    printf("Hello world!\n");

    // Terminate the program.
    return 0;
}

The functionality provided in C is split up into what are known as header files. When a particular piece of functionality is required in a program the relevant header file needs to be incorporated using the ‘#include’ statement. This is discussed in more detail in the next section.

The text in green above are comments. When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. The first comment above is a multi-line comment. Multi-line comments must start with ‘/*’ and end with ‘*/’. For single line comments, such as the second example above, the comment must be preceded by ‘//’.

The words in blue are known as reserved words, or keywords, which have a specific purpose in the language and can’t be used for anything else.

All programs written in C have a function called ‘main’, where execution of the program begins. The code within the ‘main’ function is encompassed with opening and closing curly brackets, or curly braces, as they are sometimes called.

Finally, the ‘return’ statement terminates the ‘main’ function and therefore the program as a whole.

In terms of compiling and running the program, this will vary depending on the software being used to create it. The Code::Blocks IDE, mentioned previously, has a ‘Build’ menu, which contains a ‘Build’ option to compile the program and a ‘Run’ option to execute the program. There is also a ‘Build and Run’ option that completes both steps. If software is being used that doesn’t come with a compiler, then one will need to be installed separately and compilation may have to be done via the command line.