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?

- 973
- 7
- 17
- 37
-
2You can name the executable whatever you want regardless of the `.c` that the source code is in. – Hunter McMillen Dec 16 '12 at 05:24
2 Answers
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:

- 132,704
- 33
- 254
- 328
-
-
1Do 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
-
-
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
-
-
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
-
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.

- 400,186
- 35
- 402
- 621
-
2Assuming the executable is named the same as the C source file. – Hunter McMillen Dec 16 '12 at 05:24
-
1-1 `gcc myprogram.c -o differentname` --> "differentname.c" I don't see why suggesting this is a good idea at all. – Jonathon Reinhart Dec 16 '12 at 05:30
-
Yes, I have - but your first paragraph is making a wild assumption that even the OP didn't make. – Jonathon Reinhart Dec 16 '12 at 05:33
-
@AlexeiLevenkov What header file should I use for _FILE_, Sir? I'm on linux (C program). – Gomathi Dec 16 '12 at 05:48
-
@Gomathi Please note the underscores, it's `__FILE__`. And no header file is needed, it's part of the pre-processor that processes the source before the compiler proper. Also see my second link for other macros that you can use. – Some programmer dude Dec 16 '12 at 05:54
-
-
@Gomathi Then you don't use the _underscores_. Two underscores before, two after, no spaces. – Some programmer dude Dec 16 '12 at 05:59