I am making a basic calculator to get familiar with passing arguments. The function I coded is:
int op(char* sig, int num1, int num2)
{
char sign=*sig;
if(sign == '+') return num1+num2;
if(sign == '/') return num1/num2;
if(sign == '-') return num1-num2;
if(sign == '*') return num1*num2;
return 0;
}
My main function grabs the numbers and the sign and calls the "op" function.
int main(int argc, char *argv[])
{
int num1=atoi(argv[1]);
int num2=atoi(argv[3]);
char* sig=argv[2];
int res=op(sig,num1,num2);
printf("%d\n",res);
}
The problem is when I call the program through the terminal using the "./" command it doesn't take the *
sign as a char. Instead when I use *
in the arguments, that argument becomes pipewriter.c
which is the name of the file where I coded this. For example, ./pw.exe 3 + 4
prints 7
while ./pw.exe 3 * 4
just prints 0.
How can I use the *
sign as just a '*'
character?