10

I did not find any limitation of count function parameters in the C99 standard and I guess it is only limited by stack size.

However I've written a simple test program to demonstrate the behavior of a function with a large count of parameters. When its about 10k, I get the following error on gcc (gcc version 4.5.3 on Cygwin):

/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../libcygwin.a(libcmain.o):(.text+0xa9): undefined reference to `_WinMain@16'

I realize that such large count of parameters is unlikely but I wonder what parameter of the compiler determines this limit?

EDIT

script to generate C-source

#!/bin/sh

num=$1

echo "" > out.c
echo "#include <stdio.h>" >> out.c

echo "int getsum( " >> out.c

i=0
while [ $i -lt $num ]
do
    ((i++))
    if [ $i -eq $num ] 
    then
        echo "int p$i )" >> out.c
    else 
        echo -ne "int p$i," >> out.c
    fi
done

echo "{" >> out.c

echo -ne "  return " >> out.c

i=0
while [ $i -lt $num ]
do
    ((i++))
        if [ $i -eq $num ]
        then
                echo "p$i;" >> out.c
        else
                echo -ne "p$i + " >> out.c
        fi
done

echo "}" >> out.c

echo "int main(){"  >> out.c
echo "printf(\"Sum of %d elements is %d\", $num, getsum(" >> out.c 

i=0
while [ $i -lt $num ]
do
        ((i++))
        if [ $i -eq $num ]
        then
                echo "$i" >> out.c
        else
                echo -ne "$i," >> out.c
        fi
done

echo "));" >> out.c

echo "return 0;}" >> out.c
gcc out.c
./a.exe
triclosan
  • 5,578
  • 6
  • 26
  • 50

1 Answers1

16

The Standard specifies a certain minimum number which every implementation must support,

5.2.4.1 Translation Limits

— 127 parameters in one function definition
— 127 arguments in one function call

jørgensen
  • 10,149
  • 2
  • 20
  • 27
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Interesting. That's not a whole lot! I would have thought "unlimited, depending on memory size" too. – Mr Lister Jan 27 '12 at 14:56
  • 2
    These are the *minimum acceptable limits* an implementation may impose. It's certainly allowable (and desirable) to have a larger limit or no explicit limit at all on things like this. – R.. GitHub STOP HELPING ICE Jan 27 '12 at 16:33
  • 2
    There is also the limit on the maximum length of a source line, 1K-something, or maybe 4K-something. The test program will likely hit this limit, too. (whatever comes first ...) – wildplasser Jan 27 '12 at 17:00
  • 15
    Not to mention there is a *maximum acceptable limit* imposed by the humans who have to read your code, and exceeding this limit can cause Undefined Behavior in those persons. So don't stretch it :) – jørgensen Jan 27 '12 at 17:43
  • @wildplasser Does "source line" mean a line that is terminated with a CR/LF, or a line that is terminated with a semicolon? The first would be a limit that is easy to avoid. The latter limit would be harder to avoid. – Craig McQueen Mar 03 '14 at 00:21
  • That's way more than it should be. I'd say the standard should cap it at 8. – Brandon Yates Nov 06 '15 at 16:15
  • @R..: It's necessary that there exist at least one possibly-contrived program that uses 127 arguments and which the compiler processes in standard-compliant fashion. A function which ignores all 127 arguments and unconditionally returns zero would probably suffice to meet that requirement. I don't think anything in the Standard requires that the implementation must correctly process a function that accepts 127 arguments and actually does anything with them. – supercat Aug 05 '16 at 21:24