I've started to learn C and in the chapter on "user input" there is an example calculator simply using command-line arguments. When I wrote and compiled it on my own machine, all the operators (-, +, /) except the multiplication (*) operator worked. When trying to multiply it just displays the command prompt on a new line. Any reason for this? Could the example be wrong? Here it is:
#include <stdio.h> int main (int argc, char *argv[]) { int arg1, arg2; if (argc == 4) { sscanf (argv[1], "%d", &arg1); sscanf (argv[3], "%d", &arg2); if (*argv[2] == '+') printf ("%d\n", arg1 + arg2); if (*argv[2] == '-') printf ("%d\n", arg1 - arg2); if (*argv[2] == '*') printf ("%d\n", arg1 * arg2); if (*argv[2] == '/') printf ("%d\n", arg1 / arg2); } return 0; }
./a.out 2 \* 3to prevent the*being expanded to all the files in the current directory.