Digital Garden
Computer Science
C & System Programming
Introduction to C

Introduction to C

History

The C programming language was originally developed at Bell Labs (like so many other things) by Dennis Ritchie in 1972 to construct utilities running on Unix. Later on, it was also used to re-implement the entire kernel of the Unix operating system and has till this day been the kernel language for Unix. In 1989 C was standardized by ANSI (American National Standards Institute) to so-called ANSI C, also known as C89. Later on, in the same year, it was then adopted by the International Organization for Standardization (ISO) to create the so-called Standard C. Over the years ISO has published new standards corresponding to the years in which they were published: C90, C99, C11, C17 and now they are working on C2x.

Running a C Program

When writing a C program there are 4 phases of programming:

  • Editing: Writing and modifying your code.
  • Compiling: This part is split into 2 phases:
    • Preprocessing: The code can still be modified by the compiler.
    • Compilation: The compiler checks the syntax and semantics of your code and makes sure everything is in order. The compiler then translates the code to assembly language which is then further translated into actual machine code/instructions. These machine instructions are then stored in object files that have either the extension .obj or .o.
  • Linking: The goal of this phase is to get the program into its final form for execution. The linker combines the object modules with additional libraries needed by the program to create the entire executable file which can then be run.
  • Running: This is the final phase and is self-explanatory.

For more about the compilation and linking phase check out this article (opens in a new tab).

First C Program

helloWorld.c
#include "stdio.h"
 
int main(void)
{
    printf("Hello World"); // prints "Hello World"
    return 0;
}

To then compile and run our "Hello World" we can use for example the GNU Compiler Collection (gcc).

foo@bar:~$ gcc -std=c11 -pedantic -pedantic-errors -Wall -Wextra -g -o helloWorld helloWorld.c
foo@bar:~$ helloWorld.exe
Hello World

The options mean the following:

  • -std=c11 use C11 standard or can you use -std=c89, -std=c99, -ansi.
  • -pedantic use strict ISO C warnings.
  • -pedantic-errors use strict ISO C errors.
  • -Wall shows all warnings.
  • -Wextra turn on extra warnings.
  • -g activates the debugger.
  • -o the name of the executable file.

Commenting

Comments are the same as in many other programming languages like Java (inspired by C/C++). A single-line comment starts with // and is written over a line. A multi-line comment is written between /* ... */ and can span over multiple lines.

Include

The #include <stdio.h> is a preprocessor directive that tells the compiler that we want something done in the preprocessing phase. All preprocessor directives start with a #. The "include" instruction tells the compiler to include the contents of the "stdio.h" file. You might notice it has the .h extension which means it is a header file. Header files define information about functions, so-called function prototypes which describe a function so the functions name, its arguments etc. The file we are including stands for standard input output which is part of the C standard library and we use the printf function of that file to write to the standard output, which is by default the console.

When specifying the file to be included you can either write it between double quotes or angle brackets. The difference between these two forms is subtle but important. If a header file is included using < >, the preprocessor will search a predetermined directory path to locate the header file (the folder for the standard library). If the header file is enclosed in "", the preprocessor will look first for the header file in the same directory as the source file and then in the other folders.