0

I wrote a program that can accepts arguments from the command line, which are supposed to be a number, an operator (of length 1) and another number:

int main(int argc , char *argv[]){
    if (argc < 4){
        printf("Too few arguments (4 required)");
        return -1;
    }

    char *a = argv[1];
    char b = *argv[2];
    char *c = argv[3];

    printf("%s %c %s = ...", a, b, c);
    return 0;
}

Which seems to work fine, such as when I pass 110 + 11 I get:

110 + 11 = ...

But if I pass something with the operator *, like 110 * 11, it messes up:

110 b c_nums.cbp = ...

The only thing that seems to work is if I replace * with ^* or '*' and change char b = *argv[2]; to char b = *(argv[2]+1);

I'm assuming this has to do with how cmd or whatever formats commands, but how can I fix this without always passing ^<op> instead of <op>?

Edit: Ok, so it looks like "*" works in cmd but not Code::Blocks for some reason.

EpichinoM2
  • 137
  • 2
  • 10

1 Answers1

1

The * character has special meaning to the shell. It gets interpreted before your program is even executed.

You need to escape the * from the shell in order for your program to see it, either by preceeding it with a backslash or enclosing it in quotes.

dbush
  • 205,898
  • 23
  • 218
  • 273