请问C-读取命令行参数怎么读取

我编写了很少的程序来计算pi(π)作为整数。现在,我面临一个问题,如何扩展它以计算积分,在启动应用程序时将作为附加参数给出该积分。如何在程序中处理此类参数?


慕森卡
浏览 577回答 3
3回答

ibeautiful

#include <stdio.h>#include <stdlib.h>int main(int argc, char **argv) {&nbsp; int i, parameter = 0;&nbsp; if (argc >= 2) {&nbsp; &nbsp; /* there is 1 parameter (or more) in the command line used */&nbsp; &nbsp; /* argv[0] may point to the program name */&nbsp; &nbsp; /* argv[1] points to the 1st parameter */&nbsp; &nbsp; /* argv[argc] is NULL */&nbsp; &nbsp; parameter = atoi(argv[1]); /* better to use strtol */&nbsp; &nbsp; if (parameter > 0) {&nbsp; &nbsp; &nbsp; for (i = 0; i < parameter; i++) printf("%d ", i);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; fprintf(stderr, "Please use a positive integer.\n");&nbsp; &nbsp; }&nbsp; }&nbsp; return 0;}
打开App,查看更多内容
随时随地看视频慕课网APP