First Program

First "C" Program:

#include
main()
{
printf("Hello World\n");
}

The first line is the standard start for all C programs - main(). After this comes the program's only instruction enclosed in curly brackets {}. The curly brackets mark the start and end of the list of instructions that make up the program - in this case just one instruction.

Notice the semicolon marking the end of the instruction. We might as well get into the habit of ending every C instruction with a semicolon - it will save you a lot of trouble. C is very unfussy about the way you lay it out.

The printf function does what its name suggest it does: it prints, on the screen, whatever we tell it to. The "\n" is a special symbols that forces a new line on the screen.

Save it as Hello.c. Then use the compiler to compile it, then the linker to link it and finally run it. The output is as follows:

Hello World

Add Comments to a Program

A comment is a note to yourself that we put into our source code. All comments are ignored by the compiler. They exist solely for our benefit. Comments are used primarily to document the meaning and purpose of our source code, so that we can remember later how it functions and how to use it. We can also use a comment to temporarily remove a line of code. Simply surround the line(s) with the comment symbols.

In C, the start of a comment is signalled by the /* character pair. A comment is ended by */.Comments can extend over several lines and can go anywhere except in the middle of any C keyword, function name or variable name.

For example:
/* This is a comment. */

Lets now look at our first program one last time but this time with comments:

main() /* main function heading */
{
printf("\n Hello, World! \n"); /* Display message on */   /* the screen */
}