Tuesday, January 5, 2016

The Importance of the main() Function in C Programming

By Dan Gookin from C All-in-One Desk Reference For Dummies
All C language programs must have a main() function. It's the core of every program. It's required. The main() function doesn't really have to do anything other than be present inside your C source code. Eventually, it contains instructions that tell the computer to carry out whatever task your program is designed to do. But it's not officially required to do anything.

The basic main() function
When the operating system runs a program in C, it passes control of the computer over to that program. This is like the captain of a huge ocean liner handing you the wheel. Aside from any fears that may induce, the key point is that the operating system needs to know where inside your program the control needs to be passed. In the case of a C language program, it's the main() function that the operating system is looking for.

At a minimum, the main() function looks like this:

main() {}
Like all C language functions, first comes the function's name, main, then comes a set of parentheses, and finally comes a set of braces, also called curly braces.

If your C program contains only this line of code, you can run it. It won't do anything, but that's perfect because the program doesn't tell the computer to do anything. Even so, the operating system found the main() function and was able to pass control to that function — which did nothing but immediately return control right back to the operating system. It's a perfect, flawless program.

Dissecting the main() function
The set of parentheses after a C language function name is used to contain any arguments for the function — stuff for the function to digest. For example, in the sqrt() function, the parentheses hug a value; the function then discovers the square root of that value.

The main() function uses its parentheses to contain any information typed after the program name at the command prompt. This is useful for more advanced programming. Beginning programmers should keep in mind what those parentheses are there for, but you should first build up your understanding of C before you dive into that quagmire.

The braces are used for organization. They contain programming instructions that belong to the function. Those programming instructions are how the function carries out its task or does its thing.

By not specifying any contents, as was done for the main() function earlier, you have created what the C Lords call a dummy function — which is kind of appropriate, given that you're reading this at Dummies.com.

Note that the basic, simple main()function doesn't require a specific keyword or procedure for ending the program. In some programming languages, an END or EXIT command is required, but not in C. In the C language, the program ends when it encounters the last brace in the main() function. That's the sign that the program is done, after which control returns to the operating system.

0 comments:

Post a Comment