Use “*” as argument of function main()

懵懂的女人 提交于 2019-12-02 07:37:46

问题


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 

回答1:


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.




回答2:


You have to escape the * while giving the input. \*. So we have to mention to shell that is not a wild card. It is character.



来源:https://stackoverflow.com/questions/27578605/use-as-argument-of-function-main

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!