Tuesday 7 February 2012

Main() Function

Dear Visitors,
           In this lesson you will learn about main() function of a program and also their uses in C....
SO FRIENDS LETS START OUR LESSON
  • Main() Function:-
                              The main() function indicates the beginning of the actual C program. It is the point at which execution of program is started. When a C program is executed, the execution control goes directly to the main() function.
                Every C program must have the main() function. The main() function comes after the preprocessor directives.
  •       The general syntax of main() function is:
                       void main(void)
{
                                           Body of main() function
}
In the above syntax:
1). The keyword 'void' is used before main() function. It indicates the data type of the value that is returned by the main() function. By definition, a function may accept one or more parameters and returns a single value.
          The program is executed with in an operating system (e.g, DOS). At the end of program execution, the main() function can return a value to the operating system. The 'void' means that program will not return any value to the operating system after its execution.
          You can use "int" in place of 'void'. In this case, program will return an integer value. The operating system uses this value to determine whether the program is executed successfully or not.
2). The keyword 'void' used within brackets after the main() function indicates that program takes no parameters when program is executed from command line (i.e. DOS prompt).
       Note: 
                     The use of 'void' or 'int' before the main() function are the main body of the main() function are optional.
3). The statements written under the main() function are the main body of the main() function. The body of the main() function is enclosed in braces ( or curly brackets { } ). These braces are called delimiters. The left brace indicates the start of the body of the function. whereas the matching right brace indicates the end of the body of the function.

No comments:

Post a Comment