gpt4 book ai didi

c - 我的 C 中的 for 循环出现奇怪的错误

转载 作者:行者123 更新时间:2023-11-30 15:06:45 25 4
gpt4 key购买 nike

我正在用 C 编写一个程序,计算输入的每个单词中的字母数量,然后将它们打印为直方图。我在这个程序中使用了很多 for 循环,并且出现以下错误:

letter_size_chart.c:35:37: error: expected expression
for (i = maximum; i >= MIN_CHARS; --i)
^
letter_size_chart.c:38:27: error: expected expression
for (j = MIN_CHARS; i <= MAX_CHARS; ++i)
^
letter_size_chart.c:48:23: error: expected expression
for (i = MIN_CHARS; i <= MAX_CHARS; i++)
^
letter_size_chart.c:50:23: error: expected expression
for (i = MIN_CHARS; i <= MAX_CHARS; i++)
^
4 errors generated.

我的循环中是什么导致了这些错误?这是我的代码:

/* 
sorts input by size of words into a histogram
*/

#define EOF -1
#define MAX_CHARS 10 /* max number of chars allowed in a word */
#define MIN_CHARS

#include<stdio.h>
#include<ctype.h>

int main()
{
int c, i, j, word_length, numbcountsize, maximum;
int numbcount[MAX_CHARS];
word_length = 0;

while ((c = getchar()) != EOF)
{
if (isalpha(c))
++word_length;
else
if (word_length != 0)
{
++numbcount[word_length - 1];
word_length = 0;
}
}

maximum = numbcount[0];
for (i = MIN_CHARS; i <= MAX_CHARS; i++)
if (numbcount[i - 1] > maximum)
maximum = numbcount[i];

for (i = maximum; i >= MIN_CHARS; --i)
{
printf("%d |", i);
for (j = MIN_CHARS; i <= MAX_CHARS; ++i)
{
if (j >= i)
printf(" * ");
else
printf(" ");
}
printf("\n");
}
printf(" | ");
for (i = MIN_CHARS; i <= MAX_CHARS; i++)
printf("_");
for (i = MIN_CHARS; i <= MAX_CHARS; i++)
printf("%d\n", i);
}

最佳答案

当您使用空字符串执行#define时,即

#define MIN_CHARS

它告诉预处理器从程序文本中删除所有提及MIN_CHARS的内容。实际上,您的循环如下所示:

for (i =; i <= 10; i++) 

这是无效的,因此 C 编译器拒绝它。

MIN_CHARS 提供值将解决此问题:

#define MIN_CHARS 2

关于c - 我的 C 中的 for 循环出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38809047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com