0Pricing
C Academy · Lesson

argc and argv

Reading program arguments.

Programs receive arguments

When you run a program from a terminal, you can pass it extra words called command-line arguments.

C delivers them to main through two parameters: argc (argument count) and argv (argument vector).

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("This program received %d arguments\n", argc);
    return 0;
}

The main signature

The full form of main is:

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

argc is the number of arguments. argv is an array of string pointers, one per argument.

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("argc = %d\n", argc);
    return 0;
}

All lessons in this course

  1. argc and argv
  2. Parsing Options
  3. getopt
  4. Building a CLI Tool
← Back to C Academy