5

I would like to display my current C file using C program. I know that the executable name can be obtained by argv[0]. But I want the file name as 'hello.c' for example. Is it possible?

Gomathi
  • 973
  • 7
  • 17
  • 37

2 Answers2

20

A single program can be comprised of several C files. So which one are you looking for?

The __FILE__ macro was made for this reason. It is replaced by the preprocessor with the name of the current source (.C) file being compiled.

main.c

#include <stdio.h>
int main(int argc, char** argv)
{
    printf("Executable name: %s\n", argv[0]);

    printf("This is %s() from %s, line %d\n",
        __FUNCTION__, __FILE__, __LINE__);

    one();

    return 0;
}

one.c

#include <stdio.h>
void one(void)
{
    printf("This is %s() from %s, line %d\n",
        __FUNCTION__, __FILE__, __LINE__);
}

Output (assuming executable name is "hello.exe"):

Executable name: hello.exe
This is main() from main.c, line 4
This is one() from one.c, line 3

See also:

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • What header file should I use for _FILE_, Sir? I'm on linux. – Gomathi Dec 16 '12 at 05:47
  • 1
    Do you mean `__FILE__`? None. Again, these macros are defined by the *preprocessor* (which runs before the actual compiler.) They are not defined by any header file. – Jonathon Reinhart Dec 16 '12 at 05:50
  • But, when I use it, it says _FILE_ undeclared. I'm on linux. – Gomathi Dec 16 '12 at 05:51
  • Again, you're saying `FILE` - do you mean `__FILE__` with two leading and trailing underscores? I get that you are on linux. What compiler are you using? GCC? – Jonathon Reinhart Dec 16 '12 at 05:52
  • Yes, Sir. With underscores. I'm using GCC. – Gomathi Dec 16 '12 at 05:55
  • You are doing something wrong. I **just** compiled the example code above and `__FILE__` **definitely** works. Two underscores, before and after. No spaces. Or just copy and paste the code. It works. – Jonathon Reinhart Dec 16 '12 at 05:57
  • Thanks, Sir. I just didn't note the two underscores. I put only one. That's why the error. Thank you so much. – Gomathi Dec 16 '12 at 06:04
  • Yep. Okay let's delete all these comments, so they don't detract from the question. – Jonathon Reinhart Dec 16 '12 at 06:23
  • 1
    @JonathonReinhart - Great answer. As an aside printf("This is %s() from %s, line %d\n", __FUNCTION__, __FILE__, __LINE__) makes for great run-time error messages. No question what's failing and where it is. –  Dec 16 '12 at 07:59
  • @RocketRoy Thank you. And you've just made me realize why I was so confused with Gomathi's error messages. His were in *italics* because he was incorrectly using only one underscore. You, however, correctly used two underscores, and your strings became bold :-) – Jonathon Reinhart Dec 16 '12 at 08:05
  • Backticks. `¯\_(ツ)_/¯` – Slipp D. Thompson Jan 31 '15 at 23:15
1

If you're on Windows, and have the ".exe" extension to your program, then replace that extension with ".c". If you're on Linux or OSX, then just append ".c", but do not append directly to argv[0] as that string won't have space allocated for that. Create a new string instead.

This will however not work in all situations, as the actual source file and executable may of course by named differently, and the executable may not even be in the same folder as the source file. Getting the actual name of the source file can be done with the __FILE__ macro. If you are using VisualC++ then you can add a flag to the compiler telling it to use full path in the __FILE__ macro, see this MSDN reference. The GCC pre-processor already have the full path in __FILE__, see the documentation.

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