它所做的是将参数 ./a.out -b 101
转换为十进制数 5,但我想做的只是将 101 转换为二进制数,如果我给它参数 -b
#include <stdio.h>
void main(int argc, char *argv[])
{
if ((argc == 3) && (argv[2] == "-b") ) //here lies the problem
{
int n = 0;
char *c_pt = argv[2];
printf("argv[1]: %s\n", argv[1]);
while (*c_pt)
{
if (*c_pt < '0' || *c_pt > '1')
{
break;
}
n = n * 2 + *c_pt - '0';
c_pt++;
}
printf("%d\n", n);
}
}
最佳答案
在 C 中你需要调用 strcmp(3)
比较字符串。
关于c - 在 C 程序中读取命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5688853/