Question

Is there a limit on the number of arguments that we pass to main() in C? As you all know, it is defined as int main(int argc, char *argv[]).

When I call the program, I can pass arguments like so:

$ prog.exe arg1 arg2 arg3.....argn

Is there an upper bound in the number of argument that we may supply to main() in this way?

Was it helpful?

Solution

No, there is no limit imposed by the ISO C99 standard. If you're using the "blessed" main form (of which there are two):

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

then you will be limited to the maximum size of a signed integer (implementation-dependent but guaranteed to be at least 215-1 or 32,767).

Of course, you could even have more than that since the standard specifically allows for non-blessed main forms (for example, one that takes a long as the count).

The standard mandates how the arguments are stored and things like argv[argc] having to be NULL, but it does not directly limit the quantity.

Of course, there will be a limit in practice but this will depend entirely on the implementation and environment. However, if you have to ask, then you're probably doing something wrong.

Most tools would place a truly large number of arguments into a response file (say args.txt) then pass a single argument like:

my_prog @args.txt

which gets around arbitrary limits on argument quantity and size.

OTHER TIPS

According to the POSIX spec for exec, there is a macro ARG_MAX defined in <limits.h> which defines the maximum number of bytes for the arguments + environment variables.

But since C doesn't define anything about that, no, there isn't an inherent cross-platform limit. You have to consult your OS manual if it doesn't define that macro.

I wouldn't think so. While there may not be a theoretical limit, the computer probably can't handle 1.5 million arguments. Is there any particular reason you need to know this? I wouldn't recommend using command line arguments for thing other than options, file parameters, ect...

There is no limit explicit in C itself. This is an example of a behavior not defined in the language but rather the implementation. Remember that the language itself is different than it's implementation, subsequent libraries, IDE's, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top