C Books Guide and List
C++ Books Guide and List
Best Java Books

3,749 questions

4,049 answers

114 comments

10,705 users

What is the purpose of main() function?

0 votes
31 views
asked Jan 8, 2014 by sara Guru (24,570 points)

1 Answer

0 votes
In C, program execution starts from the main() function. Every C program must contain a main() function. The main function may contain any number of statements. These statements are executed sequentially in the  order which they are written.

The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of  function is reached. In C, the function prototype of the 'main' is one of the following:

int main(); //main with no arguments

int main(int argc, char *argv[]); //main with arguments

The parameters argc and argv respectively give the number and value of the program's command-linearguments.

Example:

#include <stdio.h>

/* program section begins here */

int main() {

// opening brace - program execution starts here

printf("Welcome to the world of C");

return 0;

}/

/ closing brace - program terminates here

Output:

Welcome to the world of C
answered Jan 8, 2014 by rajesh Guru (39,140 points)

Related questions

0 votes
1 answer 42 views
0 votes
1 answer 3,800 views
3,800 views asked Jul 9, 2011 by pax Sr Member (3,190 points)
0 votes
1 answer 52 views
+1 vote
1 answer 103 views
103 views asked Jul 12, 2011 by steve Sr Member (1,460 points)
0 votes
1 answer 20 views
...