main()
コマンドライン引数がない場合にのみ、このプログラムに印刷を実行させたいと思います。コマンドライン引数(1つの整数である必要があります)がある場合は、関数を実行する必要がありますbitcount()
。
どうすればこれを行うことができますか?コマンドライン引数がない場合、これがどのように正しく機能するかわかりません。
ユーザーがコマンドライン引数を入力したかどうかを確認するにはどうすればよいですか?そして、もしそうなら、実行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
}