Program name: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added C example)
 
No edit summary
Line 4: Line 4:
=={{header|C}}==
=={{header|C}}==
C has difficulty accessing source code filenames, because C code must be compiled into an executable. However, C can access the executable's filename.
C has difficulty accessing source code filenames, because C code must be compiled into an executable. However, C can access the executable's filename.



<lang c>int main(int argc, char **argv) {
<lang c>int main(int argc, char **argv) {

Revision as of 23:55, 5 August 2011

Program name is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

It is useful to programmatically access a program's name, e.g. for determining whether the user ran "python hello.py", or "python hellocaller.py", a program importing the code from "hello.py".

C

C has difficulty accessing source code filenames, because C code must be compiled into an executable. However, C can access the executable's filename.

<lang c>int main(int argc, char **argv) { printf("Executable: %s\n", argv[0]);

return 0; }</lang>