-3

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?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
nope
  • 223
  • 4
  • 15

2 Answers2

6

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

jgr
  • 3,914
  • 2
  • 24
  • 28
0

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;
 }
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78