some more questions about argv

M

Momo

so if i want to see like if argv[1] is starts with a hyphen (-), do i go like
if (strncmp(argv[1], "-",1)==0)
is this correct to check of argv[1] begins wif a hyphen??
or is there something special for the hyphen
any help will be good
 
K

Keith Thompson

so if i want to see like if argv[1] is starts with a hyphen (-), do
i go like
if (strncmp(argv[1], "-",1)==0)
is this correct to check of argv[1] begins wif a hyphen??
or is there something special for the hyphen

Yes, that will work (and no, there's nothing special about hyphen,
it's just another character).

Since you're specifically checking for a single character you can also do:

if (argv[1][0] == '-')

First, of course, you need to check the value of argc to make sure
that argv[1] exists.

You might also be able to use system-supplied argument parsing
routines. The C standard doesn't define any such routines, but you
can see whether your system provides "getopt" or something similar.
 
A

Al Bowers

Momo said:
so if i want to see like if argv[1] is starts with a hyphen (-), do i go like
if (strncmp(argv[1], "-",1)==0)
is this correct to check of argv[1] begins wif a hyphen??
or is there something special for the hyphen
any help will be good

if(argv[1][0] == '-')

or

if(*argv[1] == '-')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top