The Main Function, The Heart Of The Program

 Main Function In C++ Program : The Heart Of The Program.

Here we are  discussed about the main function. Which are very important topic and we will also see about how we use main function with various method. How we use main function in C++ with arguments and what's the benefits of the use of main method in C++

First question is why do you need a main function in C++ ?

The main Function Declaration : 

We don't declare the main function. The main function is inbuilt function into the specific language. In every C++ program we must write the program with the main function. If we write the program the compiler gives an error. Compiler gives this type of the error like (Dynamic-link libraries and static libraries don't have a main function.) that's why we said that the main function is the heart of the every program. The main function is where your source code begin with the execution.  Before the execution of the source code we check there all of the member are declare and initialize before use in the program. And in your mind one question is that can main function be overloaded in C++ ?

Some Restriction While Use The main Function In Our Program :

  1.  main function can't be overloaded.
  2.  main function can't be declared as a inline function.
  3.  Also we can't declare static as main function in C++ but we declare static in java. 
  4. And we can't take the address of the main function.
  5. The most important thing is that we can't call main function in any function of our program.
So you note all above important point and take care of all point while write the main function in C++ example.

How Does  main Function in C++ Is Different From C ?  

In main function of C we don't specify any return type value. So we can write the return type of the main function in C is void. But the same thing in C++ we create then we write the return type of main function in our program is int (means integer)
that means return an integer value.

#include<iostream>
using namespace std;
int main()
{
    cout <<"Hello World"; 
    return 0;      //valid for the C++ but not valid for C.
}

Here we note that some online compiler or even code blocks support that if we don't write the return 0 than also fine and compile your program. Because the compiler of all IDE add by default return 0; And in your mind one question has that why we use return 0 in C++ ?Because if we compile your program and any error occur than no problem compiler shows that this error is happen, but if error is not occur and no output generate than how you predict that my program is successfully compile or not. So if you write return 0 in your code than in last your program return 0, that's point you know that your program successfully compile.

The above all parts are described that how we use main function without any argument. Now we discussed that how we use the main function with arguments.

main Function With Arguments :

We use command line argument with C and C++. You declare command line after the name of the program and in the command line shell of the operating system. In the command line arguments the command is provided by the users. 

T0 pass the command in main function we provide the two parameter in the function : first argument is the number of the command and second argument is list of the command line arguments.

Syntax :

int main(int argc, char *argv[]) 
{
     /* ... */ 
}

OR

int main(int argc, char **argv)
 { 
    /* ... */
 }

argc(Argument Count) : argc is int and store the command line argument which is entered by the user also including the name of the program. Therefor we pass the  argument there are total 2 arguments. (first is a command and second is a program name)
  • The value of the argc is non-nagative.
argv(Argument Vector) : argv is the array of thee character pointing by the pointer to listing of all the arrays.
  • If user provide the argc greater than zero, than the elements of the array start with the argv[0] to argv[argc-1]. which contain the list of the elemets pointer in string.
  • the first index argc[0] is the name of the program. after that till argc-1 is all the elements of the array. 

For better understand you can run this program in your machine.

// Name of program commandline.cpp 
#include <iostream> 
using namespace std; 
  
int main(int argc, char** argv) 
    cout << "You  entered " << argc << " arguments:" << "\n"; 
  
    for (int i = 0; i < argc; ++i) 
        cout << argv[i] << "\n"; 
  
    return 0; 

Some Properties Of Command Line Arguments :

  1. The command line argument passed in the main function.
  2. The argument is supplied when the program is invoked.
  3. argv[argc] is a null pointer.
  4. The first index of argv[0] hold the name of the program.
  5. argv[1] point the first argument and argv[n] point the last argument.



    Comments

    Popular posts from this blog

    Datatypes In C++

    Implicit And Explicit Type Conversion

    Operators in C++