Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

main: starting and ending a program

The main function in the global namespace is the entry point of an ordinary C++ program. It returns a termination status to the environment, not text for display.

Version: C++23

Syntax

int main()
int main(int argc, char* argv[])

Use int main() without parameters or int main(int argc, char* argv[]) to receive startup arguments. argc is their count, and argv points to the first element of an array of pointers to strings. Check argc > 1 before reading argv[1]. void main() is not a portable form.

return 0; indicates successful termination. Reaching the end of main has the same effect. The return value is not printed: output needs a separate operation. Returning destroys the function's automatic objects before subsequent program termination processing.

Do not call main like an ordinary function, including recursively, or put it inside your own namespace. Move shared work into a helper function. This description covers a hosted environment; some embedded systems have implementation-defined startup rules. The example needs <iostream> for output, not for the declaration of main itself.

Examples

#include <iostream>

int main() {
    std::cout << "CKTIS\n";
    return 0;
}

Result

CKTIS

Sources