Command Line Arguments in C

Get Certified in C Programming for Free and Take Your Skills to the Next Level

The main() function is the entry point for all C programs. It is responsible for initializing the program and executing code. One important feature of main() is that it can accept command-line arguments – parameters that are passed to the program when it is invoked from the terminal. This allows the program behavior to be dynamic based on input from the user.

Command-line arguments allow you to control C programs from the terminal. You can pass data, options or commands into your program when you run it. This provides flexibility compared to hard-coding values inside the source code. In this guide, we will explore the syntax and usage of command-line arguments in C.

Definition of Command Line Arguments in C

Command line arguments consult with the parameters provided by the person when executing an application through the working machine’s command line interface. In the past, we generally used the principle() characteristic with no parameters.

However, when working with command line arguments, you need to outline the primary() feature with two parameters. The first parameter suggests the count of command line arguments, and at the same time, the second parameter represents the real listing of command line arguments surpassed through the consumer.

Syntax for Command-Line Arguments in C

The main() function can be defined to accept command-line arguments in the following syntax:

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

Here, argc and argv are special parameters provided to main():

  • argc – This is an integer value representing the number of arguments passed to the program from the command line.
  • argv – This is an array of character pointers listing all the arguments.

An alternative syntax for argv is to use a double pointer char **argv.

Understanding argc and argv

Let’s look closer at the argc and argv parameters:

  • argc stands for “argument count”. It contains the number of arguments passed to the program from the command line. This will be a non-negative integer value. For example, if the user invoked the program with 3 arguments, argc would be 3.
  • argv stands for “argument vector”. It is an array of character pointers that point to the actual arguments.

The elements in the argv array range from argv[0] to argv[argc-1]. Each detail is a C-style string representing a controversy.

For example, argv[0] includes the primary argument, that is, the program calls itself. Argv[1] will point to the second argument, and so forth.

Example: Using Command-Line Arguments in C

Example 1:

Here is a simple C software that demonstrates the usage of command-line arguments:

#include <stdio.h>

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

  printf("Program name %s\n", argv[0]);

  if(argc > 1) {
    printf("There are %d arguments:\n", argc);

    // Print all arguments
    for(int i=0; i < argc; i++) {
      printf("Argument %d: %s\n", i, argv[i]);
    }
  }

  else {
    printf("No arguments were passed.\n");
  }

  return 0;
}

Let’s give an explanation of what this software does:

  • It prints the program call from argv[0].
  • It tests if argc is greater than 1, which means at least one argument turned into passed.
  • In that case, it prints the overall quantity of arguments in argc.
  • Then, it loops through each detail of argv and prints the argument index and price.

Now, let’s compile and run this program with some sample arguments:

$ gcc args.c -o args
$ ./args apple 10 20 30
Program name ./args
There are 4 arguments:  
Argument 0: ./args
Argument 1: apple
Argument 2: 10
Argument 3: 20 
Argument 4: 30

As you can see, the arguments we passed on the command line were available in argv for the program to use. The argc parameter contained the total number of arguments.

Example 2:

#include <stdio.h>

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

  printf("\nProgram name: %s\n", argv[0]);

  if(argc < 2) {
    printf("\nNo command line arguments passed!\n\n");
  }
  else {
    printf("\nCommand line arguments:\n"); 

    for(int i=1; i < argc; i++) {
      printf("Argument %d: %s\n", i, argv[i]);  
    }
    
    printf("\n");
  }

  return 0; 
}

Sample Output:

Without arguments:
Program name: ./program
No command line arguments passed!

With arguments:
Program name: ./program
Command line arguments:
Argument 1: arg1
Argument 2: arg2
Argument 3: arg3

The key points are:

  • Print program name from argv[0]
  • Check argc for arguments
  • Loop through argv to print each argument
  • Format output neatly with newlines and spaces
    This demonstrates a clean way to access command-line arguments in C suitable for an article. The code is properly indented and formatted, and the sample output is boxed to showcase the result.

 Properties of Command Line Arguments in C

Here are some key properties and characteristics of command-line arguments in C:

  • When the programme starts, arguments are sent to the main() function.
  • The values come from parameters supplied when running the program.
  • This allows you to control program behavior from the outside instead of hard-coding values.
  • argv[argc] will always be a NULL pointer to indicate the end.

Some other notes:

  • argv[0] always points to the name of the program itself.
  • argv[1] points to the first “real” argument supplied.
  • argv[argc-1] points to the last argument.
    If arguments have spaces, they must be enclosed in quotes when passed.

 Uses of Command Line Argument in C

  • Command-line arguments are passed to main() function as argc and argv[]
  • argc indicates the number of arguments, argv is an array of argument strings
  • argv[0] is program name, argv[1] first argument, argv[2] second, etc
  • Arguments can be retrieved as strings and converted to ints or doubles
  • Useful for automating tasks by passing input data, options, and configs to programs

Conclusion

Command-line arguments provide a useful way to make C programs flexible. Instead of hard-coding input, you can accept input dynamically when the program runs. The argc and argv parameters give you access to these arguments in main(). While command-line args may seem complex at first, with some practice you’ll find them very useful for writing robust programs in C. I encourage you to experiment with them on your own programs!

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *