0

This web page says:

C supports variable numbers of arguments.

Okay, I got that.

But there is no language provided way for finding out total number of arguments passed.

Really? Is that true?

Apparently it is true because in every example that I have seen of a function with a variable number of arguments, there is always an argument that specifies the number of arguments, e.g.,

int sum(int numargs, ...)

Isn't that cheating?

Isn't the whole point of a function with a variable number of arguments is for the function to figure out how many arguments are provided? (Not be told how many arguments are provided)

Is there no way to create a function that sums an arbitrary number of integers, without the caller telling the function how many integers there are? (And without forcing the caller to append some sort of sentinel value like NULL onto the end of their argument list)

Is this possible:

int sum(...)
14
  • 4
    Yes, it's really true. But I'm not sure what kind of answer would be useful to you. I can answer your questions one by one (yes it's true, no there is no way to sum an arbitrary number of integers, etc), but the only supporting evidence I can give is "read the entire C standard and you won't find any provision that makes this possible". Commented May 29, 2021 at 17:21
  • 1
    I think that is just how C works. It doesn't do anything for you. You have to do that yourself. Commented May 29, 2021 at 17:23
  • 1
    I think instead of using ... you can just use an array. Commented May 29, 2021 at 17:25
  • 1
    @Goion - arrays don't have length information embedded in them. Commented May 29, 2021 at 17:25
  • 2
    There is no way to know the number of arguments, or the type of the arguments. How would the number of arguments be useful if you don't even know the types? C is a simple language. If you want those features, you can use C++ variadic templates. Commented May 29, 2021 at 17:39

1 Answer 1

2

To really understand why this is true, and why there can only be workarounds like providing sentinel value or format string (like printf does), you need to look at how C functions are compiled.

https://en.wikipedia.org/wiki/X86_calling_conventions

If you look at the assembly that is generated (try gcc -S if you are on a *nix type OS), you will see there is no information regarding how many arguments are being passed, and it is up to the code of the function to pull them off the stack (usually, some conventions pass some arguments via registers).

As a matter of fact, not only the count, but the type information is lost in compilation as well.

Unlike other, newer languages, all C function has to access parameters is a stack pointer, from there it is up to the function to decide what to pop off the stack and how to interpret it.

The variadic syntax in C just allows you to bypass the rigid argument checks during compilation, and give back some of the assembly level freedom regarding function arguments.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.