According to the C++ standard, (if arc > 0
) argv[0]
is either an empty string or it represents the name used to invoke the program. The exact representation is unspecified.
According to POSIX standard, the value in argv[0]
should point to a filename string that is associated with the process being started by one of the exec functions.
Note that neither standard guarantee that the path is a relative one, like you would want it to be.
Given that command that argv[0]
is some representation of the program name, if you simply pass 11 arguments to the program, then there will be argv[0]
...argv[11]
i.e. 12 total arguments in main
. If you pass 12 arguments to the program, then there will be argv[0]
...argv[12]
i.e. 13 total arguments in main
Microsoft documentation says that argv[0]
is by convention the command with which the program is invoked. Assuming this convention, you can achieve a relative path by using a relative path to execute the program. I don't know if it is possible to configure visual studio debugger to execute the program with a relative path to the working directory.
However, it would instead be preferable to not let your program depend on whether the program was run with a relative path (such as ./filename.exe), or using an absolute path. It is a bad design to rely on one or the other. For example, if you need the name of the executable without the directory that contains it, you can use std::filesystem::path::filename
on argv[0]
. This works whether the path is absolute or relative.
In the unconventional case where argv[0]
is not a file name at all, the above won't work. There is no standard way in C++ to get the file name of the running process. You would need to rely on a OS specific way to get it.