PDA

View Full Version : Getting parameters to "main" function... C programming using Xcode




s7on3d
Jan 12, 2008, 10:48 AM
Hi everyone,
I'm pretty new to my mac, and also relatively new to programming... First year engineering student :)
I've written the following program as a command line program in C with Xcode:

int main(int argc, char *argv[]) // MAIN FUNCTION!!
{
int i;
for (i=0;i<argc;i++)
{
printf("argv[%d]: %s\n",i,argv[i]);
}
return 0;
}

Which should receive input as a set of strings from the command line when the program is run, then print it back out.

My question is how do I run the program (I assume from the terminal...) and pass on the parameters (in this case just a sentence)?

After quite a bit of playing around I've managed to successfully reach the right directory in the terminal, and open the program (using the "open" command), but I can't work out how to send it the parameters...

Thanks in advance,
Ilan



robbieduncan
Jan 12, 2008, 10:52 AM
You shouldn't use open. That will run the program via launch services and is unlikely to preserve command line arguments. You should simply do what you would do on any other Unix system: supply the full path to the command you want to execute (in this case your program). If you are in the same folder as the executable then ./<your program name> will run the program.

s7on3d
Jan 12, 2008, 11:57 AM
Thanks :)

It's hard to be a Terminal newbie...

Cromulent
Jan 12, 2008, 12:36 PM
Thanks :)

It's hard to be a Terminal newbie...

Any book on Unix will teach you all you need to know.