Reputation: 29
I am having trouble when using a "*" when comparing strings in my C program. Is there some rule that prohibits using it? Here is the troublesome piece of code:
else if(strcmp(argv[i], "*") == 0)
{
printf("pos MULT: %d", pos);
result = dubStack[pos-1] * dubStack[pos - 2];
pos += 1;
}
When running the program with the code above my print statement does not run and the result is not calculated. However if I change the character in the string compare (such as shown below; changed * to an m), the correct operation is performed and the print statement works. Is there something I'm missing when using the * for this?
else if(strcmp(argv[i], "m") == 0)
{
printf("pos MULT: %d", pos);
result = dubStack[pos-1] * dubStack[pos - 2];
pos += 1;
}
Upvotes: 1
Views: 45
Reputation: 144949
There is nothing special about *
in C strings: "*"
is an innocuous character string of length 1.
Conversely *
is expanded by the command shell to the sorted list of names of the files and directories in the current directory. To prevent this, you must quote this character on the command line. Here are different ways to do this:
$ myprog 1 1 \*
$ myprog 1 1 '*'
$ myprog 1 1 "*"
Upvotes: 2