I try to create a simple calculator with C. The numbers and operators should be parameters. I already have the main function and a calculate function:
main:
int main(int argc, char *argv[]){
long result;
long number1 = strtol(argv[1], NULL, 10);
long number2 = strtol(argv[3], NULL, 10);
result = calculate(number1, number2, argv[2]);
printf("Result: %li", result);
return 0;
}
calculate:
long calculate(long number1, long number2, char operator){
long result;
switch(operator){
case '+': result = number1 + number2; break;
case '-': result = number1 - number2; break;
}
return result;
}
When I start the program like this:
./calc 1 + 2
the result is 0. I think there is a problem with the operator parameter, because when I write '+' instead of argv[2] it works. But I dont know how to fix it, that it works with the parameter, too.