GCC (and possibly other C compilers) keep track of argument types, at least in some situations. But the language is not designed that way.
The printf
function is an ordinary function which accepts variable arguments. Variable arguments require some kind of run-time-type identification scheme, but in the C language, values do not carry any run time type information. (Of course, C programmers can create run-time-typing schemes using structures or bit manipulation tricks, but these are not integrated into the language.)
When we develop a function like this:
void foo(int a, int b, ...);
we can pass "any" number of additional arguments after the second one, and it is up to us to determine how many there are and what are their types using some sort of protocol which is outside of the function passing mechanism.
For instance if we call this function like this:
foo(1, 2, 3.0);
foo(1, 2, "abc");
there is no way that the callee can distinguish the cases. There are just some bits in a parameter passing area, and we have no idea whether they represent a pointer to character data or a floating point number.
The possibilities for communicating this type of information are numerous. For example in POSIX, the exec
family of functions use variable arguments which have all the same type, char *
, and a null pointer is used to indicate the end of the list:
#include <stdarg.h>
void my_exec(char *progname, ...)
{
va_list variable_args;
va_start (variable_args, progname);
for (;;) {
char *arg = va_arg(variable_args, char *);
if (arg == 0)
break;
/* process arg */
}
va_end(variable_args);
/*...*/
}
If the caller forgets to pass a null pointer terminator, the behavior will be undefined because the function will keep invoking va_arg
after it has consumed all of the arguments. Our my_exec
function has to be called like this:
my_exec("foo", "bar", "xyzzy", (char *) 0);
The cast on the 0
is required because there is no context for it to be interpreted as a null pointer constant: the compiler has no idea that the intended type for that argument is a pointer type. Furthermore (void *) 0
isn't correct because it will simply be passed as the void *
type and not char *
, though the two are almost certainly compatible at the binary level so it will work in practice. A common mistake with that type of exec
function is this:
my_exec("foo", "bar", "xyzzy", NULL);
where the compiler's NULL
happens to be defined as 0
without any (void *)
cast.
Another possible scheme is to require the caller to pass down a number which indicates how many arguments there are. Of course, that number could be incorrect.
In the case of printf
, the format string describes the argument list. The function parses it and extracts the arguments accordingly.
As mentioned at the outset, some compilers, notably the GNU C Compiler, can parse format strings at compile time and perform static type checking against the number and types of arguments.
However, note that a format string can be other than a literal, and may be computed at run
time, which is impervious to such type checking schemes. Fictitious example:
char *fmt_string = message_lookup(current_language, message_code);
/* no type checking from gcc in this case: fmt_string could have
four conversion specifiers, or ones not matching the types of
arg1, arg2, arg3, without generating any diagnostic. */
snprintf(buffer, sizeof buffer, fmt_string, arg1, arg2, arg3);
%x
,%d
,%s
are all format-specifiers that "tell"printf()
how to display the data; i.e. whether the stream of bits is to be displayed as a hexadecimal number or as a decimal integer or the ASCII representation. Data is data is data. :-) The programmer (using printf) is free to interpret it however he wishes to. – TheCodeArtist Aug 13 '13 at 8:06stdio.h
is not necessary to usescanf()
as Mr @mvp suggest? – Niko Aug 13 '13 at 9:27printf
,scanf
, and friends you will always need to specify the type again, if the format string is hardcoded, some compilers will at least verify for you that the types match: gcc.gnu.org/onlinedocs/gcc/… – rakslice Aug 13 '13 at 18:33