When I want to use *
as an argument of function main()
, the shell will expand it to the all files in the current directory. How to avoid this?
int main(int argc, char *argv[])
{
printf("%d\n", argc);
for(int i=0; i<argc-1; i++)
{
printf("%s \n", argv[i]);
}
printf("\n");
}
On the command line, it will output:
atlas@localhost ~/D/P/C> ./expr 2 3 4 + *
13
./expr
2
3
4
+
Command.c
Readlines.c
catlas.h
expr
expr.c
find
find.c
The *
is a special wildcard used in shell context. The shell will always expand the *
before it is actually passed to your program. To take the input of *
as a command line argument character, you can enclose the *
in quotes, like "*"
or use an escape character \*
, as suggested in other answers, to stop the expansion.
Otherwise, The expansion of *
is being performed by the shell before it is passed to your program.