argc and argv
Reading program arguments.
argc and argv is a free C Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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;
}argv[0] is the program name
The first element, argv[0], is the name (or path) used to invoke the program.
That is why argc is at least 1 even when the user passes no extra arguments.
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("Program name: %s\n", argv[0]);
return 0;
}Printing all arguments
You can loop from index 0 to argc - 1 to visit every argument string in argv.
#include <stdio.h>
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}argv is null-terminated
By the C standard, argv[argc] is guaranteed to be a NULL pointer.
This means you can loop until you hit NULL instead of using argc, though counting with argc is more common.
#include <stdio.h>
int main(int argc, char *argv[]) {
char **p = argv;
while (*p != NULL) {
printf("%s\n", *p);
p++;
}
return 0;
}Arguments are always strings
Every argument arrives as a string, even numbers. If a user passes 42, you receive the text "42", not the integer.
You must convert it yourself with functions like atoi or strtol.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc > 1) {
int n = atoi(argv[1]);
printf("Number plus one is %d\n", n + 1);
} else {
printf("Pass a number\n");
}
return 0;
}Checking argument count
Always verify argc before reading argv[i]. Accessing an index beyond the supplied arguments is undefined behavior.
A typical guard checks that enough arguments were given and prints a usage message otherwise.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s <name>\n", argv[0]);
return 1;
}
printf("Hello, %s!\n", argv[1]);
return 0;
}Using a non-zero exit code
When arguments are wrong, return a non-zero value from main to signal failure to the shell.
By convention 0 means success and any other value means an error occurred.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Need exactly 2 arguments\n");
return 1;
}
printf("Got both arguments\n");
return 0;
}Summing numeric arguments
A practical example: add up all numbers passed on the command line by converting each with atoi and accumulating a total.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int sum = 0;
for (int i = 1; i < argc; i++) {
sum += atoi(argv[i]);
}
printf("Sum = %d\n", sum);
return 0;
}Alternate argv notation
You may see char **argv instead of char *argv[]. They mean the same thing as a function parameter: a pointer to the first string pointer.
Both let you index with argv[i].
#include <stdio.h>
int main(int argc, char **argv) {
if (argc > 1)
printf("First arg: %s\n", argv[1]);
return 0;
}Echoing arguments back
The classic echo command simply prints its arguments. Here is a minimal version that joins them with spaces.
#include <stdio.h>
int main(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
printf("%s", argv[i]);
if (i < argc - 1) printf(" ");
}
printf("\n");
return 0;
}Quick Check
Test your understanding of argc and argv.
Recap
You learned how programs read command-line input:
main(int argc, char *argv[])receives the count and the array.- argv[0] is the program name; real arguments start at index 1.
argv[argc]is NULL.- Arguments are always strings; convert numbers with
atoiand validateargcfirst.
Frequently asked questions
Is the “argc and argv” lesson free?
Yes — the full text of “argc and argv” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “argc and argv”?
Reading program arguments. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “argc and argv” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- argc and argv
- Parsing Options
- getopt
- Building a CLI Tool