Let's now move to line 2 in the C program of Listing 2.1:
#include
You see that this line starts with a pound sign, #, which is followed by include. In C, #include forms a preprocessor directive that tells the C preprocessor to look for a file and place the contents of the file in the location where the #include directive indicates.
The preprocessor is a program that does some preparations for the C compiler before your code is compiled. More details about the C preprocessor are discussed in Hour 23, "The C Preprocessor."
Also in this line, you see that follows #include. You may guess that the file the #include directive asks for is something called stdio.h. You are exactly right! Here, the #include directive does ask the C preprocessor to look for and place stdio.h where the directive is in the C program.
The name of the stdio.h file stands for standard input-output header file. The stdio.h file contains numerous prototypes and macros to perform input or output (I/O) for C programs. You'll see more program I/O in Hour 5, "Reading from and Writing to Standard I/O."
NOTE
|
The C programming language distinguishes between lowercase and uppercase characters. In other words, C is a case-sensitive language. For instance, stdio.h and STDIO.H are different filenames in C. Likewise, main() and Main() are two different function names.
|
The files that are required by the #include directive, like stdio.h, are called header files because the #include directives are almost always placed at the head of C programs. Actually, the extension name of .h does mean "header."
Besides stdio.h, there are more header files, such as stdlib.h, string.h, math.h, and so on. Appendix A, "ANSI Standard Header Files," gives a list of all the ANSI standard header files.
Angle Brackets (< >) and Double Quotes (" ")
In the second line of Listing 2.1, there are two angle brackets, < and >, that are used to surround stdio.h. You may be wondering what the angle brackets do. In C, the angle brackets ask the C preprocessor to look for a header file in a directory other than the cur-
rent one.
For instance, the current directory containing the 02L01.C file is called C:\code on my computer. Therefore, the angle brackets around tell the C preprocessor to look for stdio.h in a directory other than C:\code.
If you want to let the C preprocessor look into the current directory first for a header file before it starts to look elsewhere, you can use double quotes to surround the name of the header file. For instance, when the C preprocessor sees "stdio.h", it looks in the current directory, which is C:\code on my machine, first before it goes elsewhere for the stdio.h header file.
Normally, the header files are saved in a subdirectory called include. For instance, I might install a Microsoft C compiler in the directory MSVC on my hard drive, which is labeled as the C drive. Then the path to access the header files becomes C:\MSVC\include.
The main() Function
In line 4 of Listing 2.1, you see this function:
main ()
This is a very special function in C. Every C program must have a main() function, and every C program can only have one main() function. More generic discussions about functions are given in Hour 3, "The Essentials of C Programs."
You can put the main() function wherever you want in your C program. However, the execution of your program always starts with the main() function.
In Listing 2.1, the main() function body starts in line 4 and ends in line 8. Because this is a very simple program, the main() function is the only function defined in the program. Within the main() function body, a C library function, printf(), is called in order to print out a greeting message. (See line 6.) More details about printf() are covered in Hour 5.
One more important thing about main() is that the execution of every C program ends with main(). A program ends when all the statements within the main() function have been executed.
The Newline Character (\n)
In the printf() function, one thing worth mentioning at this moment is the newline character, \n. Usually suffixed at the end of a message, the newline character tells the computer to generate a carriage-return and line-feed sequence so that anything printed out after the message will start on the next new line on the screen.
Exercise 3 in this lesson gives you a chance to use the newline character to break a one-line message into two lines.
The return Statement
All functions in C can return values. For instance, when you make a function to add two numbers, you can make such a function that returns to you the value of the addition.
The main() function itself returns a value. By default, main() returns an integer. In C, integers are decimal numbers without fraction portions.
Therefore, in line 7 of Listing 2.1, there is a statement, return 0;, that indicates that 0 is returned from the main() function and the program is terminated normally.
A nonzero value returned by the return statement tells the operating system that an error has occurred. The bigger the return value, the more severe the error.
The exit() Function
There is also a C library function, exit(), that can be used to cause a program to end. Because the exit() function is defined in a header file, stdlib.h, you have to include the header file at the beginning of your program.
Unlike main(), the exit() function itself does not return any values, but the argument to exit() indicates whether the program is terminated normally. A nonzero argument to the exit() function tells the operating system that the program has terminated abnormally.
Actually, you can replace return 0; in line 7 of Listing 2.1 with exit(0); and get a similar result after running the modified program.
Note that return and exit() can also be used in other functions. You'll see more examples in the rest of the book.
Listing 2.2 contains the program that uses exit() instead of return.
TYPE
Listing 2.2. A C program with exit().
1: /* 02L02.c */
2: #include
3: #include
4:
5: void main()
6: {
7: printf ("Howdy, neighbor! This is my first C program.\n");
8: exit(0);
9: }
After compiling the program in Listing 2.2, you should be able to run the program and get the same message, Howdy, neighbor! This is my first C program., printed out on the screen.
The void Data Type
You may notice that the void word has been added into the C program in Listing 2.2. void is a keyword for a data type in C. When a void is placed prior to a function name, it indicates that the function does not return a value.
As you have learned, the exit() function does not return any values, but, by default, the main() function does. Therefore, as shown in line 5 of Listing 2.2, void is used to modify the returning data type of main() and to make the main() function not return any value. (You'll learn more about data types in C in Hours 4, "Data Types and Names in C," and 18, "More Data Types and Functions.")
Share with your friends: |