Frage

I would like to create a C program that accepts an argument of the form

-aK

where K is some integer from 0-9.

How would I parse/specify this option?

War es hilfreich?

Lösung

You might want to check out getopt and/or getopt_long.

Andere Tipps

a simple requirement like this can be solved with getopt.

Also you can do this:

 #include <stdio.h>
 #include <string.h>
 int main(int argc, char *argv[])
 {
    char ch, a;
    int d;
    if(argc == 1) return;
    if(argc == 2){
       if(strlen(argv[1]) > 2){
       sscanf(argv[1],"%c%c%d",&ch,&a,&d);
       if(ch == '-' && a == 'a'){
          printf("%d is your number",d);
      }
    }
   }
   return 0;
 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top