The __FILE__ macro will be concatenated to the previous line by accident in C

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 the __ FILE __ macro , I expect to get a compilation error, but I just get a warning. So the output will be:

I am in file test.c: [C

  • The first line will be: "I'm in a file" concatenated with the __ FILE __ macro .
  • The second line will be undefined

Can anyone tell me why this is not a compilation error?

+3


source to share


1 answer


While the compiler can do this, for example, printf

and scanf

(since it knows about the format strings for these functions), it cannot be done at all for functions with variable arguments.

If you create a vararg function, how does the compiler know how many arguments are correct? The answer is that he cannot. Therefore, the C spec does not say that it should be a bug.



These compilers (some, not all) give warnings for printf

and scanf

only because the compiler writers are good enough to add them. This is not required.

+5


source







All Articles