How argc and argv Reach main in a C Program
The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

The argc and argv that main() receives look like ordinary function parameters, but nobody called main() with them in any sense a C programmer would recognize. The shell never calls your function. What actually happens is that the kernel writes the arguments onto a fresh stack, a few lines of startup code reads them back off, and only then does main() run with those values in its parameters. This post follows that path, then measures it: a program prints the addresses of its argument strings, the gaps between them and where the environment begins, which shows that the strings sit in one contiguous block. It targets Linux 6.12, glibc 2.40, x86-64 and GCC 14, and follows execve(2), exec(3) and the System V x86-64 ABI. For the whole story of startup, see the complete guide to program startup.
Three stages from the shell to your parameters
Stage one: the shell builds two arrays. When you type ./tool alpha "two words", the shell splits and unquotes the line and produces an array of three strings, ./tool, alpha and two words, ending with a null pointer. It also holds an array of environment strings. It calls execve("./tool", argv, envp) with these, in a child created by fork().
Stage two: the kernel copies them onto the new stack. Inside execve() the kernel replaces the process image. The arrays the caller built live in the old address space, which is about to disappear, so the kernel first copies the strings into pages it will hand to the new process: the argument strings, then the environment strings. It then lays out the new stack: the string area at the very top, below it the auxiliary vector, then the envp pointer array, then the argv pointer array, and finally argc at the lowest address, where the stack pointer will start. Every pointer in those arrays points into the string area on the same stack.
Stage three: startup code passes them to main. The program starts at _start, which on x86-64 reads argc from the top of the stack, computes the address of the argv array just above it, and passes both to __libc_start_main(), together with a pointer to main. The C library then calls main(argc, argv, envp) in the normal calling convention, with the three values in registers rdi, rsi and rdx. By then, all the work of finding the data has been done, and main() sees ordinary parameters.
Measuring the layout
The claim that the strings are contiguous is easy to check. This program prints the address of each argv string and the distance between neighbors, then shows how far the environment is from the arguments:
#include <stdio.h>
#include <string.h>
extern char **environ;
int main(int argc, char *argv[])
{
printf("argc = %d\n", argc);
printf("argv array at %p\n", (void *)argv);
printf("environ array at %p\n", (void *)environ);
for (int i = 0; i < argc; i++) {
size_t len = strlen(argv[i]);
printf("argv[%d] -> %p len=%zu", i, (void *)argv[i], len);
if (i + 1 < argc)
printf(" next is +%td", argv[i + 1] - argv[i]);
printf("\n");
}
char *last = argv[argc - 1];
printf("first env string -> %p (%td bytes after last argument start)\n",
(void *)environ[0], environ[0] - last);
return 0;
}Run it with an empty environment plus one variable and three arguments, so the numbers are small:
env -i HOME=/root ./argv_layout alpha "two words"argc = 3
argv array at 0x7ffd5e1c0a28
environ array at 0x7ffd5e1c0a48
argv[0] -> 0x7ffd5e1c1f9f len=14 next is +15
argv[1] -> 0x7ffd5e1c1fae len=5 next is +6
argv[2] -> 0x7ffd5e1c1fb4 len=9
first env string -> 0x7ffd5e1c1fbe (10 bytes after last argument start)The addresses will differ on every run, because address space layout randomization shifts the stack. The differences do not. Each + value is the length of a string plus one for its terminator: 14 plus 1 is 15, 5 plus 1 is 6. The strings are packed back to back with a single null byte between them. The first environment string begins exactly where the last argument string ends: two words has 9 characters plus a terminator, 10 bytes, and the first variable starts 10 bytes after the argument. And the pointer arrays are close together too: the argv array has three pointers and a null (32 bytes), so environ starts 32 bytes later, at ...a48 after ...a28, which means the envp array follows argv directly. That is the layout described above, measured.
What the pointer arithmetic tells you
Three consequences of that layout matter in practice.
**argv[argc] is NULL by construction.** The kernel writes a null pointer after the last argument so that the array is terminated, and the C standard requires it. Loops written as for (char **p = argv; *p; p++) rely on it and work.
The environment follows the arguments. The last argument string and the first environment string are neighbors. That is why a program that overwrites its own argv to rename itself in ps must not write past the original length: the bytes after the final argument belong to the environment. The process name post shows the safe way.
The strings are writable. They are ordinary stack memory. A program is allowed to modify argv[i] in place, which the C standard guarantees, and some programs do it to hide a password from ps output after reading it. getopt() also permutes the pointer array argv, not the strings, when it moves operands to the end.
Reading argc from outside the program
The same data is visible from a separate process, because the kernel exposes it through /proc. /proc/PID/cmdline contains the argument strings separated by null bytes, which is the string area of the stack:
./argv_layout alpha "two words" &
tr '\0' '|' < /proc/$!/cmdline; echoFor a program that stays alive (for example one that calls pause()), the output would be:
./argv_layout|alpha|two words|Every argument ends in a null byte, so the output ends with a separator after the last one. A program that has overwritten its own argv shows the new contents there, which is how tools like ps display a renamed process.
The calling convention
By the time the C library calls your function, argc, argv and envp are in registers: edi holds argc, rsi holds argv and rdx holds envp, following the System V AMD64 calling convention. That is why the signature of main() uses int for the count and pointers for the rest. If you write your own _start without the C library, you are the startup code, and you read the same values off the stack yourself: argc at the stack pointer, argv at the address just above it. The advanced posts show that.
Edge cases that surprise people
The count can be zero. A caller may pass an empty argv to execve(). The C standard allows argc of 0, in which case argv[0] is NULL. Linux 5.18 and later insert an empty string as argv[0] when this happens, but code that must be safe elsewhere checks argc before using argv[0].
Arguments are bytes, not characters. They are null-terminated byte strings in whatever encoding the caller used. A file name containing invalid UTF-8 arrives intact, and a program must not assume it can be printed or converted.
There is no way to tell from inside the program whether an argument was quoted. The shell removed the quotes before the call, so "two words" and two\ words are indistinguishable.
Argument size is bounded. A single string cannot exceed 128 KiB on Linux, and the total of arguments and environment is capped by a quarter of the stack limit; execve() fails with E2BIG beyond it.


