Command-Line Arguments
I would like to explain this topic with one question i.e Can we pass any argument in main function of C language ? Some time this question is arrise in our mind.We know main() is also a function like other functions.Can we assign any argument in main?.Main is the first function which is executing at the time of RUNNING any program from any IDE such as TURBO C, BLOCKCODE, CODELITE, DEV C++, KDevelop, Xcode, Qt Creator, NetBeans, Eclipse, CLion, Visual Studio etc. Its depend on IDEs how they provide facility to assign arguments in main function at the time of execution.
But if you are working in command line window or (cmd prompt) than we can easily place arguments at the time of execution. At the time of defining main function we must pass some arguments such as argc and argv.
Now Let we know about argc and argv.
👉argc is integer type and count the number of arguments.
👉argv is character array pointers which contain all arguments passed at run time.It is noted that the first argument is program itself.
Example 1: Write a program to implement command line argument.
#include <stdio.h>
#include<stdlib.h>void main(int argc,char *argv[ ])
{
printf("argc=%d\n",argc);
for(int i=0;i<argc;i++)
{
printf("argv[%d]=%s\n",i,argv[i]);
}
}
OutPut
argc=1 // Show 1 argument that is program itself.
argv[0] = Show the name/path of program saved (depend on IDE used)
If we provide some more input values as a argument such as [4 5 2] all values are separated by space only.
OutPut:
argc= 4
argv[0]= Show the name/path of program or program name (depend on IDE used)
argv[1]=4
argv[2]=5
argv[3]=2
Example 2: Write a program to multiply by 2 with input values by using command line arguments.
#include <stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[ ])
{
int i=0;
printf("argc=%d\n",argc);
printf("argv[%d]=%s\n",i,argv[0]);
for(i=1;i<argc;i++)
{
int m=strtol(argv[i],NULL,10); //convert string into int
printf("argv[%d]=%d\n",i,m*2);
}
}
If we provide some more input values as a argument such as [4 5 2] all values are separated by space only.
OutPut:
argc= 4
argv[0]= Show the name/path of program or program name (depend on IDE used)
argv[1]= 8
argv[2]= 10
argv[3]= 4
Example 3: Write a program to find the product all input values by using command line arguments.
#include <stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[ ])
{
int i=0,a=1;
printf("argc=%d\n",argc);
printf("argv[%d]=%s\n",i,argv[0]);
for(i=1;i<argc;i++)
{
int m=strtol(argv[i],NULL,10);//convert string into int
a=a*m;
printf("argv[%d]=%s\n",i,argv[i]);
}
printf("Product of argv=%d\n",a);
}
If we provide some more input values as a argument such as [4 5 2] all values are separated by space only.
OutPut:
argc= 4
argv[0]= Show the name/path of program or program name (depend on IDE used)
argv[1]= 4
argv[2]= 5
argv[3]= 2
Product of argv=40
Know more about pointers
- Pointers and Functions
- Pointers and Arrays
- Pointers to Pointers
- Command line Arguments
- Dynamic Memory Allocation
No comments:
Post a Comment