Why does argv[0] print different value in these two cases?

The program is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main(int argc, char* argv[])
{
	std::cout<<argc<<std::endl;
	for(int i=0; i<argc; i++)
	{
		std::cout<<argv[i]<<std::endl;
	}
	std::cout<<std::endl;
	system("PAUSE");
	return 0;
}

If I build this rename the .exe as xeta.exe on the desktop and run it directly the output says:

1
C:\Users\HAS111\Desktop\xeta.exe

However, if I actually use a command prompt to run this file on the desktop by only entering xeta and pressing enter once the directory in command line is set to desktop, all it prints is

1
xeta

Why is the output different in these 2 cases?
Last edited on
I am using windows, actually for a long time I thought I could just use the argv[0] to get the program name and path by just reading from end of string backwards to first back-slash character.

However, when I searched on google I found that people are using some OS specific API calls to get program directory name. It was confusing. Now I understand why they don't just use the argv[0].
Topic archived. No new replies allowed.