繁体   English   中英

为什么我的 C 程序给出错误的 output 但代码是正确的?

[英]Why my C program is giving wrong output but the code is right?

我在 CLion 上写了一个测试代码。 代码是:

#include <stdio.h>

int main() {
    int a, b, sum;
    printf("Enter an integer: ");
    scanf("%d", &a);
    printf("Enter another integer: ");
    scanf("%d", &b);
    sum = a + b;
    printf("Sum: %d\n", sum);
    return 0;
}

代码的 output 不像本来的样子。 代码的output为:

Enter an integer:23
 Enter another integer:12
 Sum: 35

Output 问题: 1.第一行中(:)后面会有一个空格 2. int第二行,它在一个空格之后开始,并且在(:)之后也没有空格 3.第三行也是在之后开始空间。

还有一个问题。 它显示了scanf的警告。 此处给出了警告消息图像: 警告信息

它建议我使用strtol而不是scanf 但是当我使用strtol时,它会显示错误。 我正在使用mingw 它的解决方案是什么? 请帮我。

请刷新stdout

#include <stdio.h>
int main() {
    int a, b, sum;
    printf("Enter an integer: ");
    fflush(stdout); // here
    scanf("%d", &a);
    printf("Enter another integer: ");
    fflush(stdout); // and here
    scanf("%d", &b);
    sum = a + b;
    printf("Sum: %d\n", sum);
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM