Macro is not working properly it prints only the file name

#include <stdio.h>

#define LINE_FILE ("Line %d of file %s", __LINE__, __FILE__)

int main(void) {
    const char* str = LINE_FILE;
    printf("%s\n", str);
    printf("Line %d of file %s\n", __LINE__, __FILE__);

First print statement only prints the file name and not the complete line.

>Solution :

If you want a string pre-formatted like that, here’s how you do it:

#include <stdio.h>

#define STR_IMPL(x) #x
#define STR(x) STR_IMPL(x)
#define LINE_FILE ("Line " STR(__LINE__) " of file " __FILE__)

int main() {
    printf("%s\n", LINE_FILE);

Leave a Reply