请问C语言中多余的空行是怎么回事呀?是编译器的问题吗?

有一个C语言的编程练习,是输入正整数,然后显示所有小于该数的质数。
我写出来是这样的

#include <stdio.h>

int check_prime(long unsigned checking_num);

int main(void)
{
    long unsigned entered_num;
    long unsigned checking_num;
    int count = 0;

    printf("Enter an integer, this program will show you all the prime number less than the number you entered.\n");
    while (scanf("%lu", &entered_num) == 1)
    {
        printf("There are the following prime number less than the number you entered:\n");
        for (checking_num = 2,count = 0; checking_num < entered_num; checking_num++)
        {



            if (check_prime(checking_num) == 1)
            {

                printf("%lu\t", checking_num);
                count++;
            }
            if (count % 5 == 0)
                printf("\n");
        }
        printf("\n");

        if (count == 0)
            printf("There is no prime number less than the number you entered in.\n");
        printf("\nEnter another number to continue, Q to quit.\n");
    }

    return 0;

}

int check_prime(long unsigned checking_num)
{
    long unsigned div;
    int isprime;

    for (div = 2, isprime = 1; div*div <= checking_num; div++)
    {
        if ( checking_num % div == 0)
            isprime = 0;
    }

    return isprime;
}

其中有一个每输出5个质数就输出一个换行,但是不知道为什么总会多出来很多空行,截图如下图片说明
请大佬帮忙解决一下...

3个回答

原因:程序有一个bug,以第一行举例,你运行到第五个素数时,下一个数为12同时count为5,此时if(素数)判断为false,往下执行if(count)语句为true,进入执行换行
解决方法:把判断count的if语句放在前面的if素数语句里面
望采纳

shitDjango
oneDjango 回复Steve_Ive: 是逻辑问题吧 两个if并列就要判断一个丢开前一个条件再判断一个 嵌套式满足一个if的条件下再判断一个
10 个月之前 回复
Steve_Ive
Steve_Ive 非常感谢,所以是两个并列的if语句有一定几率会跳过一个吗?
10 个月之前 回复

已测试,望采纳:

#include <stdio.h>

int check_prime(long unsigned checking_num);

int main(void)
{
    long unsigned entered_num;
    long unsigned checking_num;
    int count = 0;

    printf("Enter an integer, this program will show you all the prime number less than the number you entered.\n");
    while (scanf("%lu", &entered_num) == 1)
    {
        printf("There are the following prime number less than the number you entered:\n");
        for (checking_num = 2,count = 0; checking_num < entered_num; checking_num++)
        {



            if (check_prime(checking_num) == 1)
            {

                printf("%lu\t", checking_num);
                count++;
            if (count % 5 == 0)     //位置改了一下
                printf("\n");
            }
        }
        printf("\n");

        if (count == 0)
            printf("There is no prime number less than the number you entered in.\n");
        printf("\nEnter another number to continue, Q to quit.\n");
    }

    return 0;

}

int check_prime(long unsigned checking_num)
{
    long unsigned div;
    int isprime;

    for (div = 2, isprime = 1; div*div <= checking_num; div++)
    {
        if ( checking_num % div == 0)
            isprime = 0;
    }

    return isprime;
}
Steve_Ive
Steve_Ive 果然好了!请问这是为什么呢?是什么原理...
10 个月之前 回复

print里的 \t 制表符 自动后移一个tab的位置 去掉就ok

shitDjango
oneDjango 回复Steve_Ive: 哦哦 没看清 以为是说有多余空格
10 个月之前 回复
shitDjango
oneDjango 哦哦 没看清 以为是说有多余空格
10 个月之前 回复
Steve_Ive
Steve_Ive 加上\t是想分隔打印的数字,看起来方便一点,但是我把\t换成了空格,还是有多余的空行呀
10 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问