what is argc in c

what is argc in c

1 year ago 39
Nature

In C programming, argc and argv are parameters of the main() function that allow a program to receive input from the command line.

  • argc stands for "argument count" and is an integer that indicates how many arguments were entered on the command line when the program was started.
  • argv stands for "argument vector" and is a pointer array that points to each argument passed to the program.

Together, argc and argv allow a program to receive input from the command line and take action accordingly. For example, a program can check if there is any argument supplied from the command line and take action accordingly.

Here is an example of how argc and argv can be used in a C program:

#include <stdio.h>

int main(int argc, char *argv[]) {
   printf("Program name is: %s\n", argv[0]);

   if (argc == 1) {
      printf("No Extra Command Line Argument Passed Other Than Program Name\n");
   } 
   else if (argc >= 2) {
      printf("Number Of Arguments Passed: %d\n", argc - 1);

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

   return 0;
}

In this example, argc is used to determine the number of arguments passed to the program, and argv is used to access each argument. The program prints the program name, the number of arguments passed, and each argument passed. If no arguments are passed, the program prints a message indicating that no extra command line argument was passed other than the program name.

Read Entire Article