Sign in to follow this  

argc

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

argv is an array of c-style strings (char array, with terminating NULL), and contains the commandline parameters. The first location in the array stores the name of the program file including path. argc tells you how many strings are in the array.

So if you run your program without commanline args, then argc is one. Only the program file is given to the program.

Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
For example:

C:\MyProgram> runme /test /my /args

would run main(argc,argv) with
argc = 4
argv[4] = { "C:\MyProgram\runme.exe", "/test", "/my", "/args" }

wrong

argv = { "C:\MyProgram\runme.exe", "/test", "/my", "/args" }
argv[4] = 0

:P

Share this post


Link to post
Share on other sites
Quote:
Original post by hpolloni
Quote:
Original post by joanusdmentia
For example:

C:\MyProgram> runme /test /my /args

would run main(argc,argv) with
argc = 4
argv[4] = { "C:\MyProgram\runme.exe", "/test", "/my", "/args" }

wrong

argv = { "C:\MyProgram\runme.exe", "/test", "/my", "/args" }
argv[4] = 0

:P


Wrong.

There is no argv[4]. [wink]

Share this post


Link to post
Share on other sites
Quote:

wrong

argv = { "C:\MyProgram\runme.exe", "/test", "/my", "/args" }
argv[4] = 0

:P

Wrong.


argc = 4
argv = { "./runme.exe", "/test", "/my", "/args" }

Although argv[0] varies from OS to OS and from where you run it. However, I assume that when you run it from the command line in Windows what you type as the program name is what argv[0] ends up being. Also, I wouldn't count on argv[0] being the program name if you want your program to be portable, since it's possible to change the value of argv[0] in linux.

Share this post


Link to post
Share on other sites
/me slaps hpolloni and thedevdan
the [4] was to indicate argv was an array of size 4. As you can plainly see I wasn't using C code since there was no ; at the end of each line [grin]


ahem.....

int argc = 4;
char **argv = malloc(argc*sizeof(char*));
argv[0] = "C:\\MyProgram\\runme.exe";
argv[1] = "/test";
argv[2] = "/my";
argv[3] = "/args";


Quote:
Original post by bytecoder
argv = { "./runme.exe", "/test", "/my", "/args" }

Although argv[0] varies from OS to OS and from where you run it. However, I assume that when you run it from the command line what you type as the program name is what argv[0] ends up being. Also, I wouldn't count on argv[0] being the program name if you want your program to be portable, since it's possible to change the value of argv[0] in unix.


Actually, I believe argv[0] is the full path of the executable. Whether that executable by the actual file or the name of the link to the file (in the case of *nix).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by joanusdmentia
Actually, I believe argv[0] is the full path of the executable. Whether that executable by the actual file or the name of the link to the file (in the case of *nix).


Nay. It's whatever command you used to launch the program, be it program.exe or /usr/../usr/../usr/bin/program.exe

Share this post


Link to post
Share on other sites

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this