3

This is the simplest example of my question:

#include<stdio.h>
int main() {
        printf("%s:%s\n", "I am in file" __FILE__);
}

When I forget to insert a comma before "__FILE__" macro, I expect to get a compile error, but just get a warning. So the output will be:

I am in filetest.c:[C

  • 1st string will be : "I am in file" concatenate with __FILE__ macro
  • 2nd string will be undefined

Could someone tell me why it is not a compile error?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Brightshine
  • 975
  • 1
  • 7
  • 17

1 Answers1

5

While it is possible for the compiler to do it in e.g. printf and scanf (because it knows about the format-strings for those functions), it can't be generally done for variable-argument functions.

If you create a vararg function, how would the compiler know what number of arguments is the correct? The answer is that it can't. Therefore the C specification doesn't say it has to be an error.

That compilers (some, not all) give warnings for printf and scanf is just because the creators of the compiler are nice enough to add it. It's not required.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621