0

I have to process command line input and if one of the arguments equal to "-?", I have to write an explanation of what I am making here is what my code looks like:

#include <stdio.h>
#include <string.h>
#include "header.h"
#define ul unsigned long


int main(int argc, char *argv[]){
    for(int i = 0; i < argc; i++){
        printf("%s\n", argv[i]);
    }
    if(argc > 1){
        if(strcmp(argv[1], "-?") == 0){
            printf("Here I compare 2 strings using my prototype of strcmp(). You have to pass .\\a.out and 2 strings you want to compare.\nIf you want to see their length, type -v after passing values of strings:)\n");
        }
    }
    if(argc > 3){
        if(strcmp(argv[3], "-v") == 0){
            printf("The length of the 1st string: %lu\nThe length of the 2nd string: %lu\n", strlen(argv[1]), strlen(argv[2]));
        }
    }
    char *s1 = argv[1];
    char *s2 = argv[2];
    printf("%i\n", strcmp1(s1, s2));
}

and this is what I get if I pass ./a.out -? :

zsh: no matches found: -?

So, my question is how do i process "-?"?

rioV8
  • 24,506
  • 3
  • 32
  • 49
Efim Rubin
  • 173
  • 1
  • 16
  • 6
    In _most_ shells, `?` is a _wildcard_ to match any _single_ character [in a filename]. So, `-?` will cause the shell to search the [current] directory for any _files_ that are a match (e.g. `-a`, `-b`, ...). The error message stated that _no_ matches were found. So, you'll have to escape it with either `'-?'` or `-\?` so the shell won't try to interpret `?` as a wildcard char. This is one of the reasons that I [personally] use (e.g.) `-h` for help – Craig Estey Jan 07 '21 at 22:52
  • Related : https://unix.stackexchange.com/questions/15740/how-to-specify-option-with-gnu-getopt – Den-Jason Jan 07 '21 at 22:57
  • Also see https://stackoverflow.com/questions/9642732/parsing-command-line-arguments-in-c which mentions using `argp` that handles -? – Den-Jason Jan 07 '21 at 22:59
  • 1
    The problem is not your program — it isn't getting run because the `zsh` is giving an error and doesn't run the program. Use `./a.out -\?` or similar to get the `?` to your program. Or use `-h` (or even `--help`) for help instead. There's probably an option to `zsh` to turn off the error, too. – Jonathan Leffler Jan 07 '21 at 23:43
  • 1
    Since `strlen` returns type `size_t`, it would be more portable to use `%zu` instead of `%lu` to pass it to `printf`. – aschepler Jan 07 '21 at 23:55
  • `unsetopt nomatch` iirc. – Shawn Jan 08 '21 at 00:44
  • if you're on \*nix then just use [getopt](https://www.gnu.org/software/libc/manual/html_node/Getopt.html) instead of checking manually https://pubs.opengroup.org/onlinepubs/009696799/functions/getopt.html https://en.wikipedia.org/wiki/Getopt#Using_POSIX_standard_getopt – phuclv Jan 08 '21 at 01:37

0 Answers0