answersLogoWhite

0


Best Answer

The main function in C++ is no different to any other function, other than it is the only required function; it defines the entry point of the application. The minimal main function (and therefore the minimal C++ program) is:

#include <stdio.h>

int main()

{

return(0);

}

The main function must always return an integer to the caller (even if not required by the caller). A return value of zero is usually used to indicate the program terminated successfully, however it is up to the programmer to decide what the return value actually means to the caller. It is common to return (-1) upon an unrecoverable error.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

In the C Programming language and some of its derivatives, the main() function is the single point of entry into your application. The operating system takes care of loading the program and preparing it for executing, then calls this function to hand over program flow to your application.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Function main() is the application's main routine where a program starts execution.It is the first user-written function run when a program starts.Object-oriented C++ programs consist mostly of classes, but there's always at least one C-like function: main(). main() is called more or less at the beginning of the program's execution, and when main() ends, the runtime system shuts down the program. main() always returns an int, as shown below:

int main() {

// ...Your Code goes here...

}

main() has a special feature: There's an implicit return 0; at the end. Thus if the flow of control simply falls off the end of main(), the value 0 is implicitly returned to the operating system. Most operating systems interpret a return value of 0 to mean "program completed successfully."

main() is the only function that has an implicit return 0; at the end. All other routines that return an int must have an explicit return statement that returns the appropriate int value.

Note that this example shows main() without any parameters. However, main() can optionally declare parameters so that it can access the command line arguments, just as in C.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

main() is the entry point for the program. When main() returns, the program ends. The value returned from main() is usually used to denote that the program completed successfully (zero) or that an error has occurred (non-zero). The return value can be collected by another program, script or batch file.

main() is generally used to co-ordinate all the functions in your program, usually within loop, or to simply pass control to another function. For trivial programs, main() may be the only function in your program.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

The main function is special because it defines the entry point of the application. As such, main must be declared in the global namespace. Indeed, main is the only function that must be declared in the global namespace; all other functions can be (and indeed should be) declared in any namespace we desire. When we return from main, the program terminates.

Typically, we use the main function to parse any command line switches and pass control to the relevant function(s).

In addition, the main function must always return a value of type int to the execution environment even if we do not explicitly return a value from main. As such, the following is a perfectly valid definition of main in C++ (but not in C):

int main (void) {}

Even if we do not explicitly return a value, the value 0 will be returned anyway (0 typically indicates no error). No other function in C++ behaves like this; if a return value is expected from a function other than main, it must be explicitly returned by that function.

Although some older versions of C allowed us to declare a return value of type void, this is non-standard and is not allowed in C++:

void main (void) {} // valid in some versions of C, but not in C++

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The single entry point where every C program starts to execute from

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is significance attached to the main function in C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the use of return function in C programming?

To return the exp. or const to the main fumction.


What is return means in programming?

A return statement exits the function in which it is declared and gives control to the calling code. Returning from the main function exits the program and gives control to the execution environment.


Why should a function or a variable be declared before its first use?

It is somewhat syntax of programming. But when program runs,device known as pre-processor process statements before main function. So when we use that function inside main function it will get idea and run directly without showing any error. So for compilers simplicity and fast execution purpose it is necessary to declare function before its use.


What do you know about structure programming language?

first write main function and what are the use classes which type you want then give the data members to the member functions finally you will terminate the program


Difference Main functions and user defined functions?

main function in C programming language and almost in every other main language is the main function that launched after the program starts. It takes two arguments, pointer to the first element of a pointers array (arguments) and number of arguments.If you would write your own functions and link by telling the entry point to your program you would not be able to get arguments any more. There are some code running before actually main function is launched. That code prepares the argument data for main function.But it is possible to change main function to your defined function, but that would require more knowledge of how linker (ld) and for example objcopy utility works.Note: There might be a difference in GNU and Microsoft C/C++ versions.

Related questions

What an example of a function?

In C-programming: int main (void) { return 0; }


What is the use of return function in C programming?

To return the exp. or const to the main fumction.


Why is function main special?

main() is so special because it starts the execution of program. or we can say that it is entry gate of any programming language


What do you understand by the left and right parenthesis in main method in C programming?

I understand they have the same meaning as they do for any function in C; to separate the function name from its arguments.


What is the main function of fibro cartilage?

Fibrocartilage tissue provides support and rigidity to attached/surrounding structures.


What is the function of shock cord on a rocket?

The shock cord keeps the nose cone attached to the main tube.


What significance is attached to the name main in c programming?

This is the function that the programs begins execution when it starts. A computer program needs to specify the location where execution of the program is to begin. Assembler languages generally allow specification of an entry point for a program. High level languages use as a starting point the only program or program file that is not specified as a subroutine, function, or subprogram. Since all code in C is a function or subroutine, the way to specify the starting entry point of an executable is by using the name "main". When the "main" module is combined with other modules and library entries using link, ld, bind, iewl, or whatever the systems calls its linking utility, the created program (executable or load module) will have the routine labeled "main" marked as its starting entry point.


What is return means in programming?

A return statement exits the function in which it is declared and gives control to the calling code. Returning from the main function exits the program and gives control to the execution environment.


Types of main function in C programming?

minimalist: int main (void); standard: int main (int argc, char **argv); unix-only: int main (int argc, char **argv, char **envp);


What does attached ribosomes do?

Production of proteins for the use of cell.


The endoplasmic reticulum is an interconnecting system of tubes and flattened sacs that are attached to the nuclear membrane causing the main function of what?

it's ' a &amp; b ' on plato (:


Why should a function or a variable be declared before its first use?

It is somewhat syntax of programming. But when program runs,device known as pre-processor process statements before main function. So when we use that function inside main function it will get idea and run directly without showing any error. So for compilers simplicity and fast execution purpose it is necessary to declare function before its use.