-1

Looking through MSVC's standard libraries, I see the function definition of printf() is:

printf(char const* const _Format, ...) What does the 3 periods mean?

KapowCabbage
  • 65
  • 1
  • 8

1 Answers1

-1

That defines the function as a Variadic Function (variable arguments) meaning it is a function which can take any number of arguments. This is useful for functions like printf since there is no way to determine how many arguments might be passed in

printf("%d %d", 5, 5); // two args

printf("%d %d %d", 5, 5, 5); // three args

This is just a feature of the language that allows you to pass any number of arguments to a function. Without this feature, the user would need to gather each desired parameter into an array beforehand, and pass them as a single parameter.

joshmeranda
  • 3,001
  • 2
  • 10
  • 24
  • If you look at how you implemnt varargs in c, you use the `va_start`, `va_arg`, and `va_end` macros, all they are doing is initializing memory and storing the arguments in a type called (`va_list`](http://www.cplusplus.com/reference/cstdarg/va_list). This list is then traversed using the `va_arg` macro, and freed with `va_end`. – joshmeranda Aug 23 '20 at 12:57
  • If you wanted to implement this one your own, you could allocate space for the arguments, store them, and iterate over the memory in the function body. This is why a lot of vararg functions make you specify how many arguments there are in some, way so that the function can determine how long the va_list is. This is often done either explicitly with an int parameter, ending the list in a null parameter, or in the case of `printf` using the format specifiers. – joshmeranda Aug 23 '20 at 13:00
  • This is also why you can pass more arguments to `printf` and you ask for in the format string, since they are never accessed or used at any time, but you will run into issues if you provide less, since you might be accessing either invalid or uninitialized memory. – joshmeranda Aug 23 '20 at 13:02
  • @puffin `va_start`, `va_arg`, and `va_end` are tool you can use to work with variadic functions. It's not how variadic functions are implemented. Variadic functions are implemented in the language and you cannot emulate them in user code. They aren't syntactic sugar because you cannot write a variadic function equivalent. – bolov Aug 23 '20 at 16:49
  • @bolov Of course you can implement it. If you look at the signature for [`vprintf`](http://www.cplusplus.com/reference/cstdarg/va_start/) you'll see that it takes a [`va_list`](cplusplus.com/reference/cstdarg/va_list), which is essentially just an array of an unspecified type. As you call [`va_arg`](http://www.cplusplus.com/reference/cstdarg/va_arg/) you are just grabbing the next N bytes depending on the specified type (eq int grabs 4 bytes). – joshmeranda Aug 23 '20 at 17:03
  • If you wanted to implement this on your own, you can just take a pointer to a block of memory. You can keep a pointer to where you are in memory and increment it according the expected type of the current parameter type after you are done using the current parameter. Keep doing this until you reach whatever your end condition is (N iterations, null value, etc) – joshmeranda Aug 23 '20 at 17:07
  • @puffin you cannot implement a function that takes any number of parameters without the variadic feature (i.e. the `...` in the parameter list) – bolov Aug 23 '20 at 17:42
  • @bolov I understand that you cannot do that, and syntactic sugar probably wasn't the right word choice, I'm just showing that there are ways to achieve a similar behavior without that explicit piece of syntax. Somewhat similar to rust's `?` operator replacing the old ['try'](https://doc.rust-lang.org/std/macro.try.html) macro – joshmeranda Aug 23 '20 at 17:46
  • @JaMiT I don't disagree that syntactic sugar is not the right term here. I also understand that you cannot strictly implement varargs in a way the alters function signatures. I also know that `vprintf` is not a vararg function, I was providing an example of how a workaround implementation would work differently than varargs with an established function already part of the language. I'll edit the answer to be less misleading. – joshmeranda Aug 23 '20 at 21:45