Flags with c

easiest way is to use argc and argv in your "main" function.

int main(int argc,char *argv[]);

where argc is a count of the number of parameters and argv is an array of pointers to the actual strings themselves. By default, argv[0] is the name of your application. So if you wrote a program named foo and it's main looked like this:

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

If you execute

./foo abc 123

then your output should look like:

0 == foo
1 == abc
2 == 123

The only somewhat tricky thing to remember is the argv[0] thing, since your array will actually contain 1 more element than argc tells you (since 0 is always assumed). Also, the exact format of argv[0] varies between os's. Some give you just the executable, some give you a full path (e.g. /usr/bin/foo), some vary depending on exactly how it was launched:

./foo could give you 'foo' or '/usr/bin/foo' or './foo'

../bin/foo could give you 'foo', or '/usr/bin/foo' or '../bin/foo'.

etc
 
Originally posted by WeeZer51402
in traditional unix programs what is the signifigance of "-" in front of flags

Just a convention used to more easily distinguish between flags vs values. Take "ls" as an example:

ls -l l

the line above does a long listing of files/dirs named 'l'. If you didn't have the - in front of the first l, you wouldn't be able to tell which was the flag and which the value.

You are free to use whatever you want, though convention says that you should use '-' to make your app more friendly.
 
Back
Top