The Preprocessor directives

A preprocessor is a program that processes a source file before the main compilation takes place. The C language handles directives whose names begin with a number sign #. We don’t need to do anything special to invoke this preprocessor. It is invoked automatically by the compiler when we compile the program.

The #include directive tells the compiler to include a different source-code file in your program. This directive tells the compiler to add the contents of the stdio.h file into your source file. This action of preprocessor is similar to pasting a block of text data into a document with your word processor.

Now the question arises – why add the contents of the stdio.h file to the program? Actually the stdio.h describes some functions that are used for console input/output because the C language does not provide any such facility of reading/writing directly on the console. The stdio.h stands for standard input–output and ‘.h’ stands for header file. By input we mean the information brought into the program and output sends out the information from the program. We can receive information either from a standard input device, keyboard, or from a file. Similarly we can send information either to a standard output device, VDU, or to a file.

The stdio.h provides scanf() function to receive information from a standard input device and printf() function to send information to a standard output device. That’s why it is necessary for the programs that use scanf() and printf() functions for input and output, to include the stdio.h file. Without its inclusion, the compiler would not recognize scanf() and printf() functions. However in some compilers, the inclusion of header files are optional. But in other compilers, it is necessary. Remember that files with .h extension are referred as header files.

The filename in the #include directive is enclosed by angle brackets, that is ‘’ (greater than symbol). This usage identifies header files that compiler system provides. However if you want to include your own header files then you would surround these user-defined header files within double quotes (“….”) as:

#include “userfile.h”
Here userfile.h is an user-defined header file.

The function main()

Every C program must have a main() function. The word main is followed by a pair of ordinary parentheses ‘(’ and ‘)’. Here you can not use square brackets ‘[’ and ‘]’ or even curly braces ‘{’ and ‘}’. There is nothing enclosed in the parentheses because the main function does not have any parameters. The word main() is followed by the statement(s) enclosed within the curly braces ‘{’ and ‘}’. However in C, the curly braces, along with the statements enclosed within them are referred to as a compound statement.

In some programs you may also find the following notations for main() function:

(i)    void main()
(ii)   void main(void)
(iii)  int main()

Actually the part that precedes the function main is called the function return type. The word void that precedes the function main in first and second notations specifies that this function does not return any value to the caller. Similarly the word that follows the main in second notation specifies that this function receives no argument from the function that calls it. Now look at the third notation. This third notation specifies that this function returns an int value. Therefore you have to place a return statement before the closing brace of main() function as:

….
int main()
{
….
return 1;
}

Finally one may ask you – who calls main() function? It is not you (although in fact you can call main() if you want, just as you can call other functions); but it is the startup code who is the primary customer for main() function.

The printf() Function

The printf() function is used to display a message enclosed between the double quotes, to the monitor. It is the most important function used to display text on the screen. Like any function, the printf() function has two parentheses between which comes the string to be displayed, enclosed in double quotations.

Here the printf() function is embedded into a statement whose ending is marked by the semicolon (;). Note that every statement in C is terminated by a semicolon (;). In other words, you can say that the semicolon tells the compiler that a statement is terminated. Now look at the strange character ‘\n’ that comes right after the string and before ending the double quote mark. This character is called new-line character. This new-line character (\n) brings the cursor to the beginning of the next line.


Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments