The main() Function, return statement in C Programming

Submitted by tushar pramanick on Mon, 02/25/2013 - 12:43

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.

 

 

Related Items

The #define and #undef Directives

The #define and #undef Directives

The #define directive is the most common preprocessor directive, which tells the preprocessor to replace every occurrence of a particular character string (that is, a macro name) with a specified value (that is, a macro body).

The C Preprocessor Versus the Compiler

The C Preprocessor Versus the Compiler

One important thing you need to remember is that the C preprocessor is not part of the C compiler.

What Is the C Preprocessor?

If there is a constant appearing in several places in your program, it's a good idea to associate a symbolic name to the constant, and then use the symbolic name to replace the constant throughout the program. There are two advantages to doing so. First, your program will be more readable.

Exercises : Answer the following Question

To help solidify your understanding of this hour's lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the Workshop before you move to the next lesson.

Question and Answer

    Q Why is random access to a disk file necessary?