Hello World


Now that your environment is set up, start by opening your text editor and inputting the following program. Create a directory where you are going to put your work for this book, and save this file as hello_world.c. This is your first C program!

  1. #include <stdio.h>
  2. int main(int argc, char** argv) {
  3. puts("Hello, world!");
  4. return 0;
  5. }

This may initially make very little sense. I’ll try to explain it step by step.

In the first line we include what is called a header. This statement allows us to use the functions from stdio.h, the standard input and output library which comes included with C. One of the functions from this library is the puts function you see later on in the program.

Next we declare a function called main. This function is declared to output an int, and take as input an int called argc and a char** called argv. All C programs must contain this function. All programs start running from this function.

Inside main the puts function is called with the argument "Hello, world!". This outputs the message Hello, world! to the command line. The function puts is short for put string. The second statement inside the function is return 0;. This tells the main function to finish and return 0. When a C program returns 0 this indicates there have been no errors running the program.