主函数main中变量(int argc,char *argv[ ])的含义
一直不太理解
int main( int argc, char* argv[] )
或者
int main( int argc, char** argv )
經(jīng)典例子
對(duì)于理解argv[ ]函數(shù)很管用:
假設(shè)將其編譯為 test.exe
在命令行下
/> test.exe test hello
得到的輸出結(jié)果為
3
hello
test
test.exe
main(int argc, char* argv[ ]),其中argc是指變量的個(gè)數(shù),本例中即指test和hello這兩個(gè)變量和程序運(yùn)行的全路徑名或程序的名字,argc即為3。
argv是一個(gè)char *的數(shù)組,其中存放指向參數(shù)變量的指針,此處argv[0]指向test.exe的全路徑名或test.exe,argv[1]指向test,argv[2]指向hello。
再例:
編譯該程序:gcc -o edit edit.c
運(yùn)行:〉edit
結(jié)果:請(qǐng)輸入想要編輯的文件名如:fillname
運(yùn)行:〉edit f1.txt
結(jié)果:編輯 f1.txt
執(zhí)行edit時(shí),argc為1,argv[0]指向edit
而執(zhí)行edit f1.txt時(shí),argc的值為2,argv[0]指向edit,argv[1]指向f1.txt
轉(zhuǎn)自:https://www.jianshu.com/p/77234c1618f9
總結(jié)
以上是生活随笔為你收集整理的主函数main中变量(int argc,char *argv[ ])的含义的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 豪华紫钻贵族和飞车紫钻的区别
- 下一篇: 域名解析错误如何解决