【发布时间】:2020-06-28 16:48:26
【问题描述】:
我是 C/C++ 编程的初学者。 这是我在终端中按升序显示二进制数的程序(我正在 Linux Mint 中编译)。
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
void reverse(char *x, int begin, int end)
{
char c;
if (begin >= end)
return;
c = *(x+begin);
*(x+begin) = *(x+end);
*(x+end) = c;
reverse(x, ++begin, --end);
}
int main()
{
unsigned int bitCount;
unsigned int naborCount;
printf("Число битов в наборе: ");
scanf("%d", &bitCount);
printf("\n");
naborCount = pow(2, bitCount);
char naborStr[bitCount*2];
for(int i = 0; i<naborCount; i++)
{
for(int j = 0; j<bitCount; j++)
{
if((i & (1<<j))==0)
{
strcat(naborStr, "0 ");
}
else
{
strcat(naborStr, "1 ");
}
if(j == bitCount-1)
{
reverse(naborStr, 0, strlen(naborStr)-1);
printf("%s \r\n", naborStr);
memset(naborStr, 0, sizeof(naborStr));
}
}
}
return 0;
}
This is what I see in the terminal
这个符号是从哪里来的?如何解决?
【问题讨论】:
-
这看起来像 C 代码,而不是 C++ 代码。为什么标记为
c++? -
strcat需要以 nul 结尾的参数,否则其行为未定义。 -
抱歉,已修复
-
欢迎使用 stackoverflow,请注意 here 不鼓励发布输出图像以及使用外部链接发布图像。
-
看看下面的答案是否有帮助