Home » Home » Main Function And Command-Line Arguments OF C++


The main function of any programme in C++ serves as its entry point. The operating system calls the main function to begin the execution of the programme when it is run. The main function is specified to accept two arguments, argc and argv, with a return type of int.


The number of command-line arguments that were given to the application is represented by the integer variable argc. The command-line arguments are contained in the argv array of character pointers. The name of the programme itself is always the first element of the argv array. The program’s arguments are contained in the remaining parts.

Here’s an example of a main function that takes command-line arguments:


int main(int argc, char* argv[]) {
std::cout << “Number of command-line arguments: ” << argc << std::endl;
std::cout << “Command-line arguments:” << std::endl;
for (int i = 0; i < argc; i++) {
std::cout << argv[i] << std::endl;
return 0;

The software in this illustration prints the command-line arguments to the console.


An integer value can also be returned by the main function to represent the program’s state. According to tradition, a return value of 0 denotes a successful termination, while a return value other than zero denotes an execution problem.

A C++ programme is separated into distinct translation units during compilation. A single source file and any header files it contains make up a translation unit. An object file is created by individually compiling each translation unit. The final executable programme is then created by linking the object files together.


Resolving external references between translation units is a step in the linking process. The technique by which the compiler links function and variable names to specific memory addresses in the final executable programme is known as linkage. In C++, there are three distinct linking types.

Internal linkage:

When the static keyword is used to declare a variable or function, internal linking is present. Only the translation unit in which they are defined can access variables and functions with internal linkage.

External linkage:

A variable or function has external linkage when it is declared without the static keyword. Any translation unit in the programmer has access to variables and functions with external linkages.

No linkage:

A variable or function has no linkage when it is declared with the extern keyword but without a definition. No-linkage variables and functions must be defined in the same translation unit where they are declared because they cannot be accessible from other translation units.


In C++, the main function is a program’s starting point and accepts command-line parameters. To build the final executable programme, C++ programmes are separated into discrete translation units and linked together. How variables and functions are accessed between translation units is determined by linkage.

Related Posts

Leave a Reply

%d bloggers like this: