【问题标题】:writing in c with printf and scanf not working as expected用 printf 和 scanf 在 c 中编写不能按预期工作
【发布时间】:2013-09-22 08:22:17
【问题描述】:

所以我完全是 C 的新手。我正在使用 eclipse 和 MinGW 编译器。我在使用 scanf 和 printf 函数的第二章,我的程序正在运行,但只有在我将三个整数输入到 scanf 函数后才将语句打印到控制台。

#include <stdio.h>
int main(void){
    int length, height, width, volume, dweight;

    printf("Enter the box length: ");
    scanf("%d", &length);
    printf("\nEnter the box width: ");
    scanf("%d", &width);
    printf("\nEnter the box height");
    scanf("%d", &height);

    volume = length * width * height;
    dweight = (volume + 165) / 166;

    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);

    return 0;
}

控制台输出:

8 (user input + "Enter" + key)
10 (user input + "Enter" key)
12 (user input + "Enter" key)
Enter the box length: 
Enter the box width: 
Enter the box heightDimensions: l = 8, w = 10, h = 12
Volume: 960
Dimensional Width: 6

有什么见解吗?我希望它先 printf,然后再 scanf 进行用户输入,如下所示:

Enter the box length: (waits for user int input; ex. 8 + "Enter")
Enter the box width: ...

【问题讨论】:

  • 尝试:在第一次 printf 之前调用 fflush(stdout)
  • 另外你应该检查scanf的返回值
  • 您使用的是哪个编译器?它可以在 gcc 中正常工作。
  • 代码在 GCC 4.7.1 上按预期工作。
  • fflush 在 printf 语句之后起作用

标签: c printf scanf


【解决方案1】:

只需在每个printf() 之后添加fflush(stdout);,然后再调用scanf()

#include <stdio.h>
int main(void){
    int length, height, width, volume, dweight;

    printf("Enter the box length: "); fflush(stdout);
    scanf("%d", &length);
    printf("\nEnter the box width: "); fflush(stdout);
    scanf("%d", &width);
    printf("\nEnter the box height"); fflush(stdout);
    scanf("%d", &height);

    volume = length * width * height;
    dweight = (volume + 165) / 166;

    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);

    return 0;
}

【讨论】:

  • 做到了。这是为什么呢?
  • 在您的系统上,stdout 中的文本仍然缓冲在内存中,并且一直在等待,直到您打印了足够多的内容以填充内部缓冲区,或者您明确刷新了缓冲区的内容。这是出于效率原因。缓冲输出通常是更有效的 I/O ...系统将缓冲区作为一个大块的输出发送,而不是一次发送一点点。在您的情况下,这不是您想要的行为。不同的系统在这方面的表现可能完全不同;这是依赖于实现的行为。
  • 顺便说一句,另一个可能触发刷新的事件是让您的程序退出。因此,如果您已经编程了一段时间并且从来没有这样做过,那是因为您可能从未使用过 scanf()。另一种可能性是输出流中的'\n' 可能会触发刷新。触发刷新不需要换行符,但在某些系统上可能会这样做。
【解决方案2】:

在 C 中处理脏缓冲区!!

您可以在每个 printf() 的末尾简单地包含一个 换行符(转义序列)'\n',这用于刷新缓冲区,最终启用在输出终端上的显示.(fflush(stdout)实现了同样的功能,但不需要每次调用printf()时都写,只需包含一个字符'\n')

注意:始终建议在 printf() 的引号 "" 中使用 '\n' 字符作为最后一个元素,因为除非使用刷新机制,否则数据将保留在缓冲区中,但是当 main() 函数结束时,缓冲区会自动刷新,而且,只有在临时缓冲区被刷新时,数据才会到达目的地。

我们的新代码应该如下所示:

#include <stdio.h>
int main(void){
    int length, height, width, volume, dweight;
    printf("Enter the box length: \n");
    scanf("%d", &length);
    printf("\nEnter the box width: \n");
    scanf("%d", &width);
    printf("\nEnter the box height \n");
    scanf("%d", &height);
    volume = length * width * height;
    dweight = (volume + 165) / 166;
    printf("Dimensions: l = %d, w = %d, h = %d\n", length, width, height);
    printf("Volume: %d\n", volume);
    printf("Dimensional Width: %d\n", dweight);
    return 0;
}

控制台输出:

Enter the box length: 
8
Enter the box width:  
10
Enter the box height 
12
Dimensions: l = 8, w = 10, h = 12
Volume: 960
Dimensional Width: 6

【讨论】:

  • printf() 的末尾加上一个换行符在scanf() 之前似乎对我没有任何影响。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 2023-03-24
  • 2019-02-19
  • 2012-03-06
相关资源
最近更新 更多