4 ms·
All the talk about `printf("%d\n", a);` and it's invalid due to a certain part of a language. From list of undefined behaviours (J.2). > In a context requiring
by GlitchMr 9y ago
All the talk about `printf("%d\n", a);` and it's invalid due to a certain part of a language. From list of undefined behaviours (J.2).
> In a context requiring two function types to be compatible, they do not have compatible return types, or their parameters disagree in use of the ellipsis terminator or the number and type of parameters (after default argument promotion, when there is no parameter type list or when one type is specified by a function definition with an identifier list) (6.7.5.3).
> disagree in use of the ellipsis terminator
printf is called with the following implicit type signature (due to not including stdio.h)
int (char *, int);
However, the actual printf signature is as follows.
int (const char *, ...);
While char * can be used where const char * is being used, int is not a valid replacement for ellipsis terminator. As such, the correct answer is that it's undefined behaviour.
(of course, most implementations will allow this code, but strictly speaking as a language lawyer it is undefined behaviour)
- Koshkin 9y agoInterestingly, with the K&R version of C we would not have such discussions at all - printf as well as many other functions did not require declarations. So, as long as we can assume that nobody would call printf with the format string as, say, the second argument, everything would work without anyone giving much of a thought. This is but one example of how simple tools reduce the unnecessary cognitive load.