answersLogoWhite

0


Best Answer

The main function is the entry point of your application. The minimal main() function is as follows:

int main()

{

return( 0 );

}

Clearly this does nothing but return the value zero to the caller. However, it is important to note that the main() function must return an integer to the calling program -- even if only zero. The return value may be used to indicate an error condition, with zero indicating success, but can be used for any purpose -- or it may be ignored altogether by the caller. However it is not for you to decide how the return value is used, so long as you return something. A main() function that returns void is an invalid program (some compilers may permit it, but they break the C/C++ standard if they do).

An actual main() function will normally include one or more sequential statements or function calls, or an infinite loop with a conditional expression to exit the loop. An infinite loop may be contained in another function. The loop may be used to process a message queue, for instance, which determines the program's flow of control.

The following example demonstrates a simple program containing infinite loop within main() itself, generating random numbers between 1 and 100 (inclusive), exiting the loop when the number is 1. The return value is the number of loops that were executed.

#include

#include

int main()

{

srand(( unsigned ) time( NULL ));

unsigned int loops = 0;

while( ++loops )

{

int n = rand() % 100 + 1;

std::cout << "Number: " << n << std::endl;

if( n == 1 ) break; // exit loop if n is 1.

}

std::cout << "Loops: " << loops << std::endl;

return( loops ); // return number of loops to caller.

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can you give an example Main function in Cpp?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Explain a program for this pointer in cpp?

Go to the link. You will got use of "this" keywork with simple explanation and example. http://cpp.codenewbie.com/articles/cpp/1527/Keyword_this-Page_5.html


What do the initials CBF and CPP stand for?

CPP


Write a program in cpp to print the charminar?

int main (void) { puts ("charminar"); return 0; }


When was CPP Group created?

CPP Group was created in 1980.


Clrscr does not works in dev cpp....any solution?

#include &lt;windows.h&gt; int main() { system("cls"); return 0; }


Difference between C main and CPP main?

There is no difference between the C main function and the C++ main function.They are both defined as int main (int argc, char *argv[]) {statements}.There have been different syntaxes over the years, but the end result is the same. There are also some variations, such as adding a char *envp[] argument to pass the environment, but that is not standard usage. It is also possible to have no arguments, as in int main() {statements}.


Give an example of a multi dimensional array in Cpp?

// 2 dimensional array (a sudoku puzzle): char sudoku[9][9]; // 3 dimensional array (100 sudoku puzzles): char sudokus[100][9][9]


What is CPP Studios Event GmbH's population?

The population of CPP Studios Event GmbH is 30.


When was CPP Studios Event GmbH created?

CPP Studios Event GmbH was created in 1983.


How do you write a program to illustrate the concept of extern variable?

In order to use extern you have to have at least two files. In first one, let's call it file1.cpp, you will define a variable using extern (in this case belongs to int):...extern int myVar = 0;...Then in file2.cpp file where you have main() you need to write following:extern int myVar;Do not initialize the variable in file2.cpp, or you code will not compile.


CPP Program for Implementing Knuth-morris-pratt pattern matching algorithm?

what is the pure algorithm instead of cpp program?


When did CPP start?

I believe it was in 1967