What do argc and argv mean in a C main function?

August 3, 2026 4 answers
Asked by MapleOrbit42
I'm learning C and came across this program structure: int main(int argc, char *argv[]) { char *pass; pass = getpass("Enter a password: "); printf("Password: %sn", pass); int strength = check_password(pass); printf("Password Strength: %d", strength); } I understand that main is the program's starting function, but I'm confused about `int argc` and `char *argv[]`. What do they represent, why are they included, and how would I use them when running a program? I'd also appreciate clarification on whether they are needed in this particular password example, since the code does not appear to use command-line arguments.

Answers

NimbleCactus29
One separate issue is `#include "password.c"`. Normally, source files are not included directly. Put the declaration for `check_password` in a header such as `password.h`, include that header, and compile and link `password.c` as a separate source file. Including a `.c` file can cause duplicate definitions or make larger projects harder to maintain.
Reply to this answer
QuietPanda7
`argc` and `argv` are used for command-line arguments. If you run a program like `./program hello world`, the runtime passes the words to `main` like this: - `argc` is `3`, the number of arguments. - `argv[0]` is usually `./program`, the program name. - `argv[1]` is `hello`. - `argv[2]` is `world`. - `argv[3]` is a null pointer marking the end of the array. `argv` is an array of strings, and `argc` tells your code how many entries to process. Without a length value, C arrays do not know their own size.
Reply to this answer
SilverMoth18
The names are conventional abbreviations: `argc` means โ€œargument count,โ€ and `argv` is commonly described as โ€œargument vector,โ€ meaning an array of argument values. The names themselves are not required; this is also valid: `int main(int numberOfArgs, char *argumentStrings[])` The first parameter is an `int` because it stores a count. The second is an array of `char *`, where each pointer refers to a null-terminated C string. Text such as `"123"` is still text; if you need the number 123, you must convert it separately.

CedarBlink5: So if one argument is `hello123`, does the count include each character and number separately?

SilverMoth18: No. `argc` counts arguments, not characters. `hello123` is one command-line argument, so it increases `argc` by one. Its individual characters are stored together in the string at the relevant `argv` index.

Reply to this answer
BrightKite63
In the password program shown, neither `argc` nor `argv` is used. You could therefore write the entry point as: `int main(void)` That makes it clear the program does not accept command-line arguments. You would use the longer form if you wanted to support commands such as `./program filename` or `./program --strength 3`. Also, `main` is special because it is the program's entry point. Its return value is an exit status: returning `0` conventionally means success, while a nonzero value indicates some kind of error.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions