getting the (star-symbol) as input in a c program

Viewed 74

I just wanted to code a little calculator in C on Windows using gcc 6.3.0 with the operators + - * /. It works fine, but if I choose *, it lists all files in the directory in my argv[] arrray. The first for loop is just for debug.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    for(int i = 0; i < argc; i++) {
        printf("%s\n", argv[i]);
    }
    if(argc != 4) {
        printf("\nFalsche Syntax!\n");
        exit(1);
    }
    
    double a,b,c;
    char op;

    sscanf(argv[1], "%lf", &a);
    sscanf(argv[2], "%c", &op);
    sscanf(argv[3], "%lf", &b);

    switch(op) {
    case '+':
        c = a + b;
        break;
    case '-':
        c = a - b;
        break;
    case '*':
        c = a * b;
        break;
    case '/':
        c = a / b;
        break;
    default:
        printf("\nUnbekannter Operator!\n");
        exit(1);
    }

    printf("\n\t%.5g %c %.5g = %.5g\n", a, op, b, c);
    
}

Any suggestions? Thanks in advance

2 Answers

Compiler is MinGW, which is being very helpful and adding wildcard expansion for you. But if you don't want it, you can turn it off.

int _CRT_glob = 0;
int main(int argc, char *argv[]) {

Now it doesn't expand *.

We note that in other environments, wildcards are expanded by the shell, so this is only effective on Windows. The location of the wildcard expansion (known as globbing) has upsides and downsides.

Related