C Program to Add two numbers using Command Line Arguments Parameters

C Program to Add two numbers using Command Line Arguments Parameters.


·        The command line arguments are handled using main( ) function arguments where argc refers to the number of arguments passed, and argv[ ] is a pointer array which points to each argument passed to the program.

·        The declaration for command line arguments is as follows:
                    int main(int argc, char *argv[ ])

·        argc - is the count of total command line arguments passed to executable on the time of execution.

·        argv - is the array of character string of each command line argument passed to executable on execution.
-------------------------------------------------------------------------------------------
#include<stdio.h>
void main(int argc, char *argv[ ])
{
    int i, sum = 0;
    if(argc!=3)
    {
         printf("You have forgot to type numbers");
         exit(1);
    }
    printf("The sum is : ");
    for(i=1;i<argc;i++)
    sum = sum + atoi(argv[i]);
    printf("%d", sum);
}


Output
---------------------------------------------------------------------------------------------------------
The Sum is 30.





EmoticonEmoticon