Site icon DataFlair

Command Line Arguments in C

command line argument in c

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():

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:

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

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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:

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:

 Properties of Command Line Arguments in C

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

Some other notes:

 Uses of Command Line Argument in C

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!

Exit mobile version