The command-line argument (CLA) is the parameter provided in the system upon request. Command-line conflict is an important concept in system C. It is widely used when one needs to control your system from the outside. Command-line arguments are transferred to the main () path. Argc calculates the number of arguments in the command line and argv [] is an array that contains values passed as arguments at the time of the run. The parameters passed through the command-line can be scanned in the program through command-line arguments.
Syntax:
int main(int argc, char *argv[])
Here,
argc is the integer type of argument that contains a total number of arguments passed through the command line.
argv[] is an array of character pointers, which contains all the parameters.
Example:
Input: C:\QC_Work\Projects\Geeks\GPL\C>args.exeOutput: args.exe
Input: C:\QC_Work\Projects\Geeks\GPL\C>args.exe 2Output: args.exe 2
Input: C:\QC_Work\Projects\Geeks\GPL\C>args.exe akash broOutput: args.exe akash bro
Input: C:\QC_Work\Projects\Geeks\GPL\C>args.exe geeksOutput: args.exe geeks
Below is the C++ program that accepts all the arguments from the user and prints every argument, including the file name. The program is compiled and executed successfully on Windows and Linux systems.
C++
#include <stdio.h>
void main(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
{
printf("%s ", argv[i]);
}
printf("\n");
}
Output:
Time complexity : O(n) since using a for loop
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now !
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
19 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...