A C program has only one required element, namely a function calledmain()
. This is, however, merely the minimum requirement, and no program is going to have only a main function. To illustrate more about the structure of C, let's look at a simple "hello world" program:
/* File: hello_world.c */ #include <stdio.h> main() { printf("Hello world!\n"); }The first line of this program,
/* File: hello_world.c */
, is a comment. Comments in C are enclosed in/* ... */
, and anything inside the slashes is ignored by the compiler. Comments are a very important part of programming. While it might be overkill to comment every line of a program, it is usually good practice to put a comment at the top of the program, and perhaps one before every function or constant definition. Also, any strange or possibly confusing piece of code should be commented. The text inside this comment simply gives the name of the file, in this casehello_world.c.c
extension to keep them separate from the executable files.The next piece of this simple program is the line
#include <stdio.h>
. This line is a directive to the C preprocessor. The file<stdio.h>
is a standard library, in this case of input/output functions. The#include
directive tells the compiler to add everything the specified library to your program. Libraries have two uses; the first is to avoid extra work by including functionality that has already been written. This saves you from having to write a lot of commonly used functions yourself. The second use of libraries is to split up your own program by putting different sections of it into different files. Since libraries do play these two different roles, there are two different ways to refer to them after the#include
statement. To refer to any of the standard libraries, you should put them into angle brackets, as in<stdio.h>
. For your own libraries, you should put them in double quotes, as in"mylib.h"
. As you can probably tell, the.h
extension specifies a C library (or module, but we'll get to that later).The next section of the code is where things happen. The line
main()
is, as I said above, required in every C program. For now, just take my word for it that you need the parentheses; the reason will become clear when we see more about functions. Everything in the body of the function is enclosed in curly braces, as are most blocks of code in C. The single line, then, of the actual progam is the lineprintf("Hello world!\n");
. First of all,printf()
is a function provided by the<stdio.h>
library. The "f" stands for "formatted," which we'll see more of later. For now, just know thatprintf()
prints things out on the screen. The argument of the function in this case is enclosed in double quotes. The only thing that may look funny about the stuff inside the quotes is the\n
. This is an example of an escape sequence that lets you tell the program to print something out that it otherwise wouldn't.\n
is the newline character, which adds an extra line of whitespace to the end of the output of the program. You must specify whitespace with newlines instead of carriage returns in the text of your program because the compiler ignores whitespace in your program. As far as the compiler is concerned, everything in your program could be written on a single line. The formatting and indentation of a program is intended only to make it easier to read for people. The final part of this line of code is the semicolon, which is required at the end of almost every statement in C. The only lines that do not require a semicolon are the ends of functions or control statements or after preprocessor directives.The last thing to say about this program is what its output is. The output is below (where the %'s are UNIX prompts):
%Hello world! %You've now seen your first example of a C program. It might be helpful for you to try putting it through your own compiler and running it, just to see what happens. In the next section, I'll introduce basic data types, which allow you to write more interesting and more powerful programs.
Back to Section 1: The Basics | On to Basic Data Types | |
Back to the Outline |