What is the proper declaration of main in C++?


The main() function is a global function. It is used to start the execution of program. Every program should have main(). The command line arguments argc and argv are optional.

The standard prototype of main() function is as follows.

int main()
{ body }
OR
int main(int argc, char *argv[])
{ body }

Here,

argc − Number of arguments passed to the program from the environment where program runs.

argv − pointer to the first element of an array.

The following is an example of main()

Example

 Live Demo

#include <iostream>
using namespace std;
int sum(int x, int y) {
   int s = x + y;
   cout << "The sum of numbers : " << s;
}
int main() {
   sum(28, 8);
   return 0;
}

Output

The sum of numbers : 36

In the above program, the code is present in sum() to calculate the sum of two numbers.

int sum(int x, int y) {
   int s = x + y;
   cout << "The sum of numbers : " << s;
}

The main() function is invoking function sum()

sum(28, 8);

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

75 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements