← Back to C Course | Chapter 12: Advanced Topics | Lesson 5 of 20

C Command Line Arguments

Command line arguments are extra words typed after a program's name when starting it, like giving a helper extra instructions. C gives them to main as a count and a list.
Syntax
c
int main(int argc, char *argv[]) {
  // argc: number of arguments
  // argv[0]: program name, argv[1]...: the arguments
  return 0;
}

What are Command Line Arguments?

Command line arguments let a user configure a program's behavior at launch time without editing or recompiling the source code, by typing extra words after the program name in the terminal.

This is how tools like compilers and file utilities accept filenames, flags, and options.

Example: What are Command Line Arguments?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	// Print formatted text to the screen
	printf("Program received %d arguments", argc);
	// Return 0 to signal that the program finished successfully
	return 0;
}

The argc Parameter

argc holds the total count of arguments the program was launched with, and it always includes the program's own name as the first entry, so a program run with two extra words has argc equal to 3, not 2.

Checking argc before accessing argv is essential to avoid reading past the arguments that were actually supplied.

Example: The argc Parameter

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	// Print formatted text to the screen
	printf("%d", argc);
	// Return 0 to signal that the program finished successfully
	return 0;
}

The argv Parameter

argv is an array of C-string pointers holding the actual argument text, where argv[0] is conventionally the program's own invocation name and argv[1] onward are the arguments the user actually typed.

Each entry is a plain null-terminated string, so you use standard string functions to parse or compare them.

Example: The argv Parameter

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	// Print formatted text to the screen
	printf("%s", argv[0]);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Reading Argument Values

To use the arguments passed at launch, index into argv starting from position 1 (skipping the program name at index 0), converting each string to a number with atoi() or similar if the argument represents numeric input rather than text.

Process them in the order the user supplied them, since argv preserves that order exactly.

Example: Reading Argument Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	char *fakeArgv[2] = {"prog", "42"};
	// Declare value and set it to atoi(fakeArgv[1])
	int value = atoi(fakeArgv[1]);
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Validating Argument Counts

Because argv is only as long as argc says it is, reading argv[i] for an i that exceeds argc-1 reads unallocated memory and can crash or behave unpredictably.

Always compare the expected argument count against argc, and print a usage message rather than proceeding, before touching any index beyond what was actually passed.

Example: Validating Argument Counts

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main(int argc, char *argv[]) {
	// Check whether argc < 2
	if (argc < 2) {
		// Print formatted text to the screen
		printf("Not enough arguments");
		// Send 1 back to the caller
		return 1;
	}
	// Print formatted text to the screen
	printf("%s", argv[1]);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting that argv[0] is the program name and the first real argument is argv[1].
  2. Reading argv[1] without first checking that argc is large enough.
  3. Using an argument as a number without converting it, since every argv entry is a string.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.