我想让这个程序在main()
中执行打印,只有没有命令行参数。 如果有命令行参数(应该只是一个整数),它应该运行功能bitcount()
。命令行参数c
我该如何去做这件事?如果没有命令行参数,我不确定这将如何正常工作。
如何检查用户是否放入命令行参数?如果他们这样做,运行bitCount()
而不是main()
。但是,如果他们不放置任何命令行整数参数,那么它只会运行main。
如./bitCount 50
应该调用bitCount
功能 但./bitCount
应该只是运行main
这是我到目前为止有:
#include <stdio.h>
#include <stdlib.h>
int bitCount (unsigned int n);
int main (int argc, char** argv) {
printf(argv);
int a=atoi(argv);
// int a = atoi(argv[1]);
printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n",
0, bitCount (0));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
1, bitCount (1));
printf ("# 1-bits in base 2 representation of %u = %d, should be 16\n",
2863311530u, bitCount (2863311530u));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
536870912, bitCount (536870912));
printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n",
4294967295u, bitCount (4294967295u));
return 0;
}
int bitCount (unsigned int n) {
//stuff here
}
的argc是参数的个数,和argv是参数数组。所以,只需检查'argc> 1'(第一个参数是文件名),然后使用'argv [1]'访问值(第一参数)。 – Supericy 2013-02-08 22:08:28
真的吗?你说你有**绝对不知道**如何做到这一点?这不可能。 – 2013-02-08 22:08:40