cfilepathmacrospreprocessor

__FILE__ macro shows full path


The standard predefined macro __FILE__ available in C shows the full path to the file. Is there any way to shorten the path and get just the filename? I mean instead of

/full/path/to/file.c

I see

to/file.c

or

file.c

Solution

  • Try

    #include <string.h>
    
    #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
    

    For Windows use '\\' instead of '/'.