Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit on the number of arguments to main in C

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?

like image 201
RaviPathak Avatar asked Sep 16 '10 07:09

RaviPathak


People also ask

How many maximum arguments main () function can take?

For C++ it's at least 256 arguments.

What is the maximum number of arguments in C?

as I know (and your first link says) the limit is 256 arguments in c++ and 127 in c. The line length is no problem since you can break a line into more lines.

How many arguments can be passed to main from the command line in C?

The C language provides a method to pass parameters to the main() function. This is typically accomplished by specifying arguments on the operating system command line (console). There are two parameters passed to main().

How many arguments can a function take C?

Answer: Any number of arguments can be passed to a function. There is no limit on this.


1 Answers

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.

like image 59
Potatoswatter Avatar answered Oct 21 '22 01:10

Potatoswatter