A C program consists of one or more functions.
In the above example, main is a function. Normally, you are free to name a function whatever you like. However, main, is a special function. Every C program must have a main, the program’s entry point.
All function definitions begin with an opening brace, {, and end with a closing brace, }.
The line preceding the definition of the function main, is a preprocessor directive. In this case, it instructs the preprocessor to replace the statement #include <stdio.h> with the contents of the file stdio.h.
The lines preceding the preprocessor directive are comment lines, provided for the benefit of the programmer.
Compiling and Executing a C Program on UNIX
- We will use the GNU C compiler to compile our programs.
- To compile a C program using our GNU compiler enter the following:
gcc <filename> -o <outputfilename>
This will create an executable with the name outputfilename. If you forget to use the –o option when compiling your code, your executable will be named a.out.
Other options are available with gcc. You are free to use those you find useful as your C and UNIX skills improve. Initially, stick with just the one.
To execute a C program on UNIX enter the following after the prompt:
./outputfilename
- An optional parameter list may follow the outputfilename.
Exercise
- Try entering this code into a UNIX file named helloWorld.c, using either vi or pico. Compile and execute the code. (Instructions for compiling and executing C programs on UNIX appear below.)