#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%d\n", argc);
}
当我输入时
file_name *
在我的电脑(操作系统为Windows 7)上,它打印出623而不是2。但在其他情况下,它会给出正确的输出。 *
是命令行参数的保留字符吗?请注意,对于以下输入,此程序会给出正确的输出:
file_name *Rafi
输出 = 2
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%d\n", argc);
}
file_name *
在我的电脑(操作系统为Windows 7)上,它打印出623而不是2。但在其他情况下,它会给出正确的输出。 *
是命令行参数的保留字符吗?请注意,对于以下输入,此程序会给出正确的输出:
file_name *Rafi
输出 = 2
yourapp *
将运行yourapp
,并将当前目录中所有非隐藏文件的名称作为参数传递。在您的情况下,有622个文件(623 = 622个文件+程序名称)。ren *.jpg *.jpeg
这样的事情成为可能,而在Unix系统上这很难做到。 - Marc Becho $f | cut -d'.' -f1
.jpeg; done”除了在文件名中有多个“.”字符时无法正常工作之外,还有什么难点吗?(实际上,这是一个很好的例子,说明了Windows方式也有优势)。 - Brian McFarland*
会被shell或运行库(*nix上是前者,Windows上是后者)展开,而不是直接使用*
,这样你会得到当前工作目录中所有文件的名称。
*
被用作通配符来匹配文件名并放置在argv
数组中。if you're using Microsoft's compiler, the C runtime will not perform globbing by default, and you would get an argc
value of 2 in your example. However, if you ask the linker to link in setargv.obj
(or wsetargv.obj
if you have a Unicode build), then globbing is added to the runtime initialization and you'll get behavior similar to Unix's. setargv.obj
has been distributed with MSVC for as long as I can remember, but it's still little known. I believe that most Windows programs perform their own wildcard expansion.
if you're using the MinGW/GCC tool chain, the C runtime will perform globbing before calling main()
(at least it does for MinGW 4.6.1 - I suspect it's been in MinGW for a long time). I think MinGW might not perform globbing for GUI programs. You can disable MinGW's globbing behavior with one of the following:
define a global variable named _CRT_glob
and initialize it to 0
:
int _CRT_glob = 0;
link in the lib/CRT_noglob.o
object file (I think this might be order dependent - you may need to place it before any libraries):
gcc c:/mingw/lib/CRT_noglob.o main.o -o main.exe
setargv.obj
的一条非常棒的信息,非常有帮助,谢谢! - Vladimir Sizikov*
扩展为当前目录中所有文件名(不以.
开头)。这与C程序关系很小,主要与shell有关。
argc
的值包括程序自身名称的1个参数,以及由shell传递的每个参数的1个参数。filename *
filename '*'
cl.exe
)将在您的两个示例中都打印 2。您使用的是不同的编译器吗? - user7116