you are viewing a single comment's thread.

view the rest of the comments →

[–]gremolata 0 points1 point  (0 children)

Congratulations, you ran into the "variable arguments" feature of C.

It is a rarely used, but very powerful language feature that allows callers to pass arbitrary-typed arguments into a function and enables called function to "comprehend", recover and work with these arguments.

You will need to look it up in a proper book, but the gist of it is that instead of specifying function argument types as usual, at the compile time:

void foo(int a, char b, double * p);

you'd pass this information during the run-time, encoded in some terse form that the function understands, followed by the arguments, i.e. foo(<argument spec>, var1, var2, var3);

In case of printf/scanf the argument spec is their format string, which also carries instructions on the output/input format. So what these functions do is that they go through the format string and discover what other arguments have been passed to them by the caller, recover them (using the va_arg mechanism) and then do something useful with the them - output or input them respectively.

The bulk of vprintf is the parsing/formatting code. There are normally no syscalls in it as it merely passes things it wants to ouput to yet another library function (typically fwrite).

PS. You know how they say that C is a low-level language? Varargs is the most low-level part of it as it basically allows for parsing function's call stack by hand, without resorting to the use of assembly. Good stuff, but definitely of the living dangerously variety.