1
A
回答
7
您可以使用argv[]
來獲取命令行參數,例如
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
if (argc != 2) // check that we have been passed the correct
{ // number of parameters
fprintf(stderr, "Usage: command param\n");
exit(1);
}
n = atoi(argv[1]); // convert first parameter to int
// ... // do whatever you need to with `n`
return 0;
}
1
int main (int argc, char *argv [ ])
{
//your code
}
argv [1]
屆時將有包含數量的數字字符串的地址。
然後,如果需要,您可以將其更改爲int
。
1
這很簡單,我希望我已經得到你的問題。請看下圖:
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("Number of arguments is: %d\n", argc);
printf("The entered value is %s\n", argv[1]);
return 0;
}
,然後編譯它在Linux上爲:
gcc file.c
./a.out 32
程序應該打印您需要的值。
希望這會有所幫助。
相關問題
- 1. unix環境中的C++還是Windows環境中的C++?
- 2. 如何將環境變量傳遞給Unix中的C++程序
- 3. 使用unix終端運行C++程序
- 4. 在UNIX環境
- 5. 在cygwin環境下用opemcv運行C程序
- 6. Unix環境下的高級編程
- 7. Microsoft Visual C++連接到Unix環境
- 8. 運行時環境
- 9. OpenJDK運行時環境與Java2運行時環境
- 10. Android的運行環境:的NoSuchMethodError崩潰我的應用程序
- 11. 在Java EE環境中運行unix腳本?
- 12. C應用程序如何在運行時在Linux環境中自行更新
- 13. 在Tomcat下運行的應用程序的環境變量
- 14. 從遠程.jar創建運行環境
- 15. Unix進程 - 編譯並運行c程序
- 16. Unix的環境變量不設置遠程執行
- 17. Unix環境中的多線程/並行Bash腳本
- 18. 如何設置python片段可以在C++程序中運行的環境?
- 19. 定義應用程序的運行環境
- 20. 生產環境中運行RoR應用程序的問題
- 21. 非靈活環境應用程序的自定義運行時?
- 22. 僅在PROD環境中運行的Symfony 2.6.1應用程序
- 23. 如何讓我的VB.NET程序在MS DOS環境下運行?
- 24. AngularJS - 查找應用程序運行的環境?
- 25. 在java 7環境下運行java 1.5編譯的程序
- 26. Bottle python程序在開發環境中運行,但不在實時環境中運行
- 27. GCC UNIX環境變量
- 28. 忘記UNIX環境變量
- 29. UNIX,得到環境變量
- 30. Linux/Unix環境變量
使用'argv',閱讀有關命令行參數。 –
謝謝你指點我正確的方向(雙關語意)。我明白我現在需要做什麼。 –
閱讀http://www.cprogramming.com/tutorial/c/lesson14.html有關C程序的命令行參數。 –