The main() Function
The most important part of a C program is its main() function. Both of the programs discussed earlier have main() functions. Although at this point the distinction is not critical, main() is a C function, not a C command. A function is nothing more than a routine that performs some task. Some functions come with C, and some are created by you. C programs are made up of one or more functions. Each program must always include a main() function. A function is distinguished from a command by the parentheses that follow the function name. These are functions:
main() calcIt() printf() strlen()
and these are commands:
return while int if float
When you read other C programming books, manuals, and webpages, the author might decide to omit the parenthesis from the end of function names. For example, you might read about the printf function instead of printf(). You’ll learn to recognize function names soon enough, so such differences won’t matter much to you. Most of the time, authors want to clarify the differences between functions and nonfunctions as much as possible, so you’ll usually see the parentheses.
The required main() function and all of C’s supplied function names must contain lowercase letters. You can use uppercase for the functions that you write, but most C programmers stay with the lowercase function name convention.
Just as the home page is the beginning place to surf a website, main() is always the first place the computer begins when running your program. Even if main() is not the first function listed in your program, main() still determines the beginning of the program’s execution. Therefore, for readability, make main() the first function in every program you write. The programs in the next several chapters have only one function: main(). As you improve your C skills, you’ll learn why adding functions after main() improves your programming power even more. Chapter 30, “Organizing Your Programs with Functions,” covers writing your own functions.
After the word main(), you always see an opening brace ({). When you find a matching closing brace (}), main() is finished. You might see additional pairs of braces within a main() function as well. For practice, look again at the long program in Appendix B. main() is the first function with code, and several other functions follow, each with braces and code.