1

In general, when using Variadic functions, we always give some sort for number of expected inputs, but never know how many are passed.

Then, how does compiler able to detect when I passed more arguments than required to printf() function ?.

Below is the sample code

#include <stdio.h>

int main()
{
    
    printf("Hello World", 2); 
 
    return 0;
}

Output:

main.c: In function ‘main’:
main.c:6:12: warning: too many arguments for format [-Wformat-extra-args]
    6 |     printf("Hello World", 2,3,4);
      |            ^~~~~~~~~~~~~
Hello World

I was using the following online compiler : https://www.onlinegdb.com/online_c_compiler

Aditya Kurrodu
  • 325
  • 2
  • 9
  • To begin with the shown code and the build message doesn't match. Please make sure that you show us the [mre] that actually caused the error or warning you ask about. – Some programmer dude Aug 09 '22 at 08:37
  • 2
    As for the "problem", the compiler simply have a check if you're calling `printf` to actually parse the format string and then match it with the passed arguments (both the count and the types). Compilers usually have such a check for `scanf` as well. – Some programmer dude Aug 09 '22 at 08:39
  • 2
    By analysing the format string. – Weather Vane Aug 09 '22 at 08:39
  • Does this answer your question? [Technically, how do variadic functions work? How does printf work?](https://stackoverflow.com/questions/23104628/technically-how-do-variadic-functions-work-how-does-printf-work) – Guy Lawrence-Downs Aug 09 '22 at 08:40
  • 4
    @GuyLawrence-Downs OP is asking how the compiler warns about arguments. – Weather Vane Aug 09 '22 at 08:42
  • 1
    @GuyLawrence-Downs No, It doesn't answer. I know that it parses string to get number of arguments but I am specifically asking how does it know I passed more arguments when no arguments are expected from the string. – Aditya Kurrodu Aug 09 '22 at 08:45
  • Is this a clue? "-Wformat-extra-args"... My compiler is old and not as stringent, so I cannot test... – Fe2O3 Aug 09 '22 at 08:47
  • @Fe2O3 I am using an online C compiler : https://www.onlinegdb.com/online_c_compiler – Aditya Kurrodu Aug 09 '22 at 08:51
  • Sorry guys I must be getting confused, I thought the issue was to do with there being too many format specifiers relative to the filling arguments like in the article. That's my bad :) – Guy Lawrence-Downs Aug 09 '22 at 08:52
  • @AdityaKurrodu The point is: the compiler reported the warning and identified the trigger that caused the warning... Its name "format-extra-args" indicates that the compilation settings for that check are enabled... Compilers are usually much more clever than most programmers, and polite, too... Take heed of what the compiler is telling you and your career will be much less stressful. – Fe2O3 Aug 09 '22 at 08:57
  • @AdityaKurrodu Change `"Hello World" to "Hello Worl%d" and the warning will disappear... :-) – Fe2O3 Aug 09 '22 at 08:59
  • 2
    @Fe2O3 The point of this question is not to disable the warning or why does it complain. It complains because I want it to complain. In typical Variadic functions, you don't know how many are arguments are passed but here the compiler got to know there is an extra argument passed. I was curious how the compiler got to know. – Aditya Kurrodu Aug 09 '22 at 09:08
  • @AdityaKurrodu Just when you start to think you 'know' something, it changes... https://www.thecodingforums.com/threads/extending-printf.969572/ – Fe2O3 Aug 09 '22 at 09:18
  • 3
    @AdityaKurrodu the compiler knows the fault better than the runtime, because it is in the position of knowing what arguments are given, and can analyse the format string to see if they match the format specifiers. But at runtime, the `printf` function will simply use arguments from where they are *expected* to be, regardless if they are correct, because it does not know how they got there. The format specifiers are a kind of promise that arguments *have* been placed on the stack, in registers, or whatever. – Weather Vane Aug 09 '22 at 09:18
  • @WeatherVane Thanks a lot, I think it answers the question more or less. – Aditya Kurrodu Aug 09 '22 at 09:26
  • The string parsing does happen at runtime too. But it has no idea whether the caller passed the correct type of argument, or any at all. The compiler isn't *required* to warn about this, but has become a feature of moderm compilers. As it happens, MSVC warns about `printf()` but not with its own function `cprintf()`. – Weather Vane Aug 09 '22 at 09:30
  • I think there's some confusion over what's being asked. From what people are saying, the compiler *handles this as a special case*, not something that is implemented as a source code construct that could be replicated in arbitrary functions created by a user of the language, w/o compiler modification. – Nathan Aug 09 '22 at 09:45

1 Answers1

1

The used compiler is smart enough(*) to recognize printf() as a special function, in this case a function of the standard library. This function receives a format string. If the compiler can read this format string, it interprets the format codes like printf() will do. And so it expects the number and types of the following arguments.

You can use this feature for your own printf-like function, for example (shamelessly copied from GCC's manual):

extern int
my_printf (void *my_object, const char *my_format, ...)
      __attribute__ ((format (printf, 2, 3)));

(*) "Smart enough" means that printf() is declared in "stdio.h" with this attribute, and the compiler knows how to process this attribute.

the busybee
  • 10,755
  • 3
  • 13
  • 30
  • 1
    Thanks a lot. So, In general, if I know the number of arguments at compile time for any user defined variadic function, we can use this feature to check the number of arguments passed (atleast with gcc) ? – Aditya Kurrodu Aug 09 '22 at 10:39
  • Please read the linked documentation: No, you cannot let the compiler check _any_ variadic function. It needs to use one of the supported methods, like for example `printf()`. And as you realize, it is compiler-dependent. -- The common way to check the correctness of your software is _to test_ it. Learn about professional testing. – the busybee Aug 09 '22 at 11:16