1

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; } 
1
  • 2
    Ahh, this is a Windows versus Linux problem. Use ./a.out 2 \* 3 to prevent the * being expanded to all the files in the current directory. Commented Jan 5, 2017 at 6:04

1 Answer 1

3

You need to use \ before * if you are using a Unix Shell otherwise The shell will understand it as a wild card character.

$ ./yourapp 1 \* 1 1 

good luck and happy coding :)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.