Maximum Arguments

Often times I poke into the C language, removing its crude veneer to look deeper into its guts — the digital protomatter that C and all programming languages devolve into when processed by the CPU. One of the things I discovered is that there is no solid limit to the number of arguments a function can have.

A typical C language function has anywhere from zero to four arguments. I’ve seen functions with five or six arguments, but no more. For functions that require additional arguments, a structure is passed instead. Into the structure are packed lots of data chunks, which makes the process convenient and less likely to screw-up.

Still, I’m amazed at how the language handles theoretically unlimited arguments. To prove that a function can take a heavy parameter load, I concocted this monster:

void arguments( int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l, int m, int n, int o, int p, int q, int r, int s, int t, int u, int v, int w, int x, int y, int z );

Yep, the arguments() function carries a burden of 26 int arguments. I don’t believe anyone in the real world would ever suffer through such a function (especially because a 26-element integer array would work just as well), but I crafted this booger to prove that it can work. Here’s source code:

#include <stdio.h>

void arguments( int a, int b, int c, int d, int e, int f,
        int g, int h, int i, int j, int k, int l, int m,
        int n, int o, int p, int q, int r, int s, int t,
        int u, int v, int w, int x, int y, int z )
{
    printf("%d %d %d %d %d %d %d %d %d %d %d %d %d\
%d %d %d %d %d %d %d %d %d %d %d %d %d\n",
            a, b, c, d, e, f, g, h, i, j, k, l, m,
            n, o, p, q, r, s, t, u, v, w, x, y, z
          );
}

int main()
{
    arguments( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
            14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26);

    return(0);
}

The arguments() function is called in the main() function, with constant values specified, integers 1 through 26.

Within the arguments() function, more overhead is taken to declare the 26 integers, and prep the printf() statement to output them, than I would ever care to see in any practical program. But, man, this is science. And the sucker works. Here’s the output:

1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 20 21 22 23 24 25 26

Now humanity is all the better for it.

Practically, the number of arguments set for a function is limited by the processor’s stack size. I assume that at some point, it’s possible to overflow the stack with data and therefore a limit on the number (and size) of arguments exists. Still, pity the poor coder who must deal with such a function.

Leave a Reply