Directives and Functions
When you write a C program in an editor (such as vi on Unix or Notepad on Windows), you are creating a source code. Every C source code has these parts:
- preprocessor directives
- main function
- body functions
Let's learn what each of these means and how they work.
Preprocessor Directives
A preprocessor directive tells the compiler to add (include) a header file before compiling the source code. A header file contains information about how the compiler should interpret the functions written in the program. Here is a simple C program that begins with a preprocessor directive:
|
#include <stdio.h> void main( ) { printf("We are learning C today."); }
|
This preprocessor directive #include <stdio.h> tells the compiler to look in the stdio.h file for instructions on how the printf function works. The header files are stored in the C library, which comes with your compiler.
Main Function
The computer will begin executing your program from the main function. For simple programs which do not return any value, we use void main ( ), like this:
|
#include <stdio.h> void main( ) { printf("We are learning C today."); }
|
|
Warning: Different compilers handle the main function differently. For example, if you are using the gcc compiler on Unix, simply type main( ) without using void. Check your compiler's notes for further instructions.
|
Body Functions
Inside the main function, we write the functions that will DO something. These functions are called body functions because they are enclosed in curly braces: { } inside the body of the main function. One popular function is printf which tells the computer to print a line of text on the computer screen:
|
#include <stdio.h> void main( ) { printf("We are learning C today."); }
|
Notice that the text is enclosed in parentheses and double-quotes: (" "). This tells the computer the difference between text and functions. Notice also that the line ends in a semicolon: ; which tells the computer to stop processing this line.
Try It
Open your plain text editor and type this source code:
|
#include <stdio.h> void main( ) { printf("We are learning C today."); }
|
Save your file as first.c and then run it through the compiler (follow the instructions provided in the compiler notation). Then link your object file to the C libraries. Then run your executable file. You should see this appear on your screen:
Did it work? If you got error messages during the compilation, go back into your editor and fix your mistakes. If you got other error messages or the program did not run, then you probably made a mistake when installing the compiler. Go back and try to figure out what went wrong. If you can't find the problem, then delete your compiler and re-install.
Add an Escape Character
There is a set of special characters, called escape characters, which add special things to your printf statements. Escape characters begin with a backslash: \ and are followed by a letter or symbol, such as:
- newline character: \n
- tab indent: \t
- beep sound: \a
The most important escape character is the newline character, which inserts a blank line in your text. The newline character is written: \n. Let's edit our first.c program to add some newline characters:
|
#include <stdio.h> void main( ) { printf("\nWe are learning C today.\n"); printf("\nNewline characters add blank lines.\n"); }
|
Save your first.c source code. Compile it. Link it. Run it. With the newline characters, your text will appear like this:
|
We are learning C today.
Newline characters add blank lines.
|
Did it work? If you got error messages during the compilation, go back into your editor and fix your mistakes.
Add a Tab
Let's try another escape character: \t, which inserts a tab (automatic spacing) in your text.
|
#include <stdio.h> void main( ) { printf("\nThere is a tab here: \t Can you see the spacing?");
printf("\nHere it is again: \t Can you see it?");
}
|
Save your source code as tab.c. Compile it. Link it. Run it. With the tabs, your text will appear like this:
|
There is a tab here: Here it is again: |
|
Can you see the spacing? Can you see it? |