Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About command line arguments of main function

It will look like int main(int argc, char *argv[]);. My questions are:

1 How many array items can I add in argv[]?

2 What is MAX size of every char *?

like image 229
javas Avatar asked Sep 21 '11 11:09

javas


People also ask

How to pass command line arguments to main () function?

Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main () with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. int main (int argc, char *argv []) { /* ... */ }

What are the command line arguments in C++?

Command line arguments in C/C++. The most important function of C/C++ is main () function. It is mostly defined with a return type of int and without parameters : int main () { /* ... */ }.

How are command line arguments handled in Python?

The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv is a pointer array which points to each argument passed to the program.

How to get the number of arguments in the command line?

In C, we can supply arguments to 'main' function. The arguments that we pass to main ( ) at command prompt are called command line arguments. These arguments are supplied at the time of invoking the program. The first argument argc is known as 'argument counter'. It represents the number of arguments in the command line.


2 Answers

You can try:

$ getconf ARG_MAX
2180000

http://pubs.opengroup.org/onlinepubs/007904975/basedefs/limits.h.html

ARG_MAX is maximum length of argument to the exec functions including environment data.

That is, there is no individual limit on the number of arguments or argument's length. Only the limit on total size required to store all the arguments and environment variables.

xargs figures out maximum command line length using sysconf(_SC_ARG_MAX); which yields the same value as reported by getconf ARG_MAX.

On Linux command line arguments and environment variables are put into new process' stack. So, the process/thread maximum stack size is the ultimate upper bound. Linux-specific limits are hardcoded in the kernel:

#define MAX_ARG_STRLEN (PAGE_SIZE * 32)
#define MAX_ARG_STRINGS 0x7FFFFFFF
like image 141
Maxim Egorushkin Avatar answered Sep 24 '22 20:09

Maxim Egorushkin


Both of those are bounded only by how much memory you have (or how much memory your OS gives your program).

EDIT: Actually, the number of arguments is also bounded by the size of int.

like image 24
flight Avatar answered Sep 26 '22 20:09

flight