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

0
0
Asked By MapleOrbit42 On

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.

4 Answers

Answered By BrightKite63 On

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.

Answered By SilverMoth18 On

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.

Answered By QuietPanda7 On

`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.

Answered By NimbleCactus29 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.