Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C Command line arguments


May 11, 2021 C


Table of contents


C Command line argument

When you execute a program, you can pass the value from the command line to the C program. These values are called command-line parameters and are important to programs, especially if you want to hardcode them from outside rather than within your code.

Command-line arguments are handled using the main() function argument, where argc refers to the number of incoming arguments, and argv is an array of pointers that point to each argument passed to the program. Here's a simple example of checking the command line for provided parameters and performing the appropriate actions based on them:

#include <stdio.h>

int main( int argc, char *argv[] )  
{
   if( argc == 2 )
   {
      printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
      printf("Too many arguments supplied.\n");
   }
   else
   {
      printf("One argument expected.\n");
   }
}

Using a parameter to compile and execute the above code, it produces the following results:

$./a.out testing
The argument supplied is testing

Using two parameters, compile and execute the above code, which produces the following results:

$./a.out testing1 testing2
Too many arguments supplied.

Without passing any parameters, compiling and executing the above code produces the following results:

$./a.out
One argument expected

It should be noted that the name of the storer, argv, is a pointer to the first command-line argument, and the last argument is the argv.n. If no arguments are provided, argc will be 1, otherwise, if a parameter is passed, argc will be set to 2.

Multiple command-line arguments are separated by spaces, but if the argument itself has spaces, you should place the argument inside the double quote "" or single quote '' when passing the argument. L et's rewrite the above examples, there is a space, so you can put them in double quotes or single quotes "" through such a point of view. Let's rewrite the above example and pass the program a command line argument placed inside double quotes:

#include <stdio.h>

int main( int argc, char *argv[] )  
{
   printf("Program name %s\n", argv[0]);
 
   if( argc == 2 )
   {
      printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
      printf("Too many arguments supplied.\n");
   }
   else
   {
      printf("One argument expected.\n");
   }
}

Using a simple parameter separated by a space, enclosed in double quotes, compiles and executes the above code, which produces the following results:

$./a.out "testing1 testing2"

Progranm name ./a.out
The argument supplied is testing1 testing2

The parameter names of the two parameters of main are as follows:

int main( int argc, char *argv[] )

It doesn't have to be written like this, it's just a convention. But it can also be written as this:

int main( int test_argc, char *test_argv[] )  

Any name you like.

But most people still write it in the beginning, as follows:

int main( int argc, char *argv[] )