Skip to main content

Your First C Program

Below is the standard first C program. We typically call this program "hello world".

#include <stdio.h>
int main(void)
{
printf ("Hello World!\n");
return 0;
}

The above program will produce the following output to the screen:

Hello World!

While this example seems pretty trivial, it contains all the important elements of a typical C program. Lets explore this program line by line.

The include Statement

#include <stdio.h>

This statement lets us use a library to help us print to the screen. std is short for standard, and io is short for input output. Thus, this is the standard input output library. By "including" it, we can make use of the functions that are part of that library. A function is a piece of code that does a task and produces a result. The functions that are part of this library have to do with producing output or accepting input. The .h is the extension of something called a header file. This header file can be thought of a list of all the functions available from the library. Thus when we include this header file, we are stating that the program can make use of any of the functions in that list.

Aside from stdio, there are a large number of libraries that we can also use. We will look at some of these in more details later in the course

The main() Function

C programs are made up of one more functions. When you start a program, it begins in the function named main (note that the spelling is exactly this including the casing!).

prototype of main

Below is the function prototype for the main() function

int main(void)

This function prototype tells us a lot about this function. We will look much closer at function prototypes in chapter 3, but for now, what you need to know about this prototype is the following

  • the function is named main
  • It does not require any command line arguments because the parameter list is void
  • When it is finished, it will give back an int (a whole number)

body of main

This particular function has a block of code contained between

We sometimes call this block of code the body of the function. It defines what the function will do.

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

printf

This next line of code calls the function printf() from the stdio library

	printf("Hello World!\n");

That is, this line of code is going to use a function that will output some text to the screen. That function is named printf and the thing it will print is "Hello World!\n". The "" are delimiters that indicate where the text begins and ends.The contents of this string is the characters between the "". Some of the characters that can't just be typed in such as newlines and tabs are written as a sequence that starts with the \ character. The \n means newline. Thus if we follow this printf() with another printf(), the next piece of output will go on the next line

return

This next line of code is called a return statement.

    return 0;

A return statement in a function indicates an exit from that function, returning to the point where the function was called. As this is the main function, a return statement would effectively end the main function and thus end the program.