answersLogoWhite

0

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

13y ago

What else can I help you with?

Continue Learning about Engineering

Write a program in cpp to print the charminar?

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


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


Why function body is delimited by braces in cpp program?

In C++, function bodies are delimited by braces {} to clearly define the scope of the function. This allows the compiler to identify which statements belong to the function, enabling proper organization of code and management of variables' lifetimes. Braces also facilitate the inclusion of multiple statements within a single function, enhancing code readability and structure.


Can you change the name of main function in C programming please explain me?

It depends on linker. Microsoft's linker has option /ENTRY which allows to set name of an entry point function. c:\>type 1.cppincludeint my_main(int argc, char* argv[]) { printf("my_main\n"); return 0; }c:\>cl 1.cpp /MD /link /SUBSYSTEM:CONSOLE /ENTRY:my_main Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved.1.cpp Microsoft (R) Incremental Linker Version 8.00.50727.42 Copyright (C) Microsoft Corporation. All rights reserved./out:1.exe /SUBSYSTEM:CONSOLE /ENTRY:my_main 1.objc:\>1 my_mainc:\>With any text-editor, but be warned: if you don't have a function by name main, your program won't build (right, if in Windows, then it is WinMain).

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]


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?


What is CPP Studios Event GmbH's population?

The population of CPP Studios Event GmbH is 30.


Is a military pension clawed back at 65 if you take cpp at age 60?

The Military Pension is clawed back at 65 whether or not you take CPP at any age or in fact even if you decide not to apply for CPP. It has to do with the amount of Military Pension contributions at the time when CPP was first brought about. In the wisdom of the political masters, it was felt that members could not afford to pay more pension per month on top of the CPP contributions. So expect to lose 25 to 30% of your current Military Pension at 65.