c - 为什么这个简单的代码带有 printf "freeze"?

标签 c

当我运行这段代码时,程序卡住了,为什么?

main()
{
int co;
    co=0;
    while (co<10) {

        co=co+1;

        if (co==3)
            printf("The number is now three.");
        if (co==7)
            printf("The number is now seven.");
        else
            printf(co);

}
}

我正在用 mingw GCC 编译它。

最佳答案

你不能像那样打印co:

你需要这样做:

printf("%d",co);

printf 将带有格式说明符的字符串作为第一个参数。后一个(可选)参数是参数本身。

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

这是一个更简洁的代码版本:

int main(){

    int co = 0;
    while (co < 10) {

        co = co + 1;

        if (co == 3)
            printf("The number is now three.");
        if (co == 7)
            printf("The number is now seven.");
        else
            printf("%d\n",co);
    }

    return 0;
}

关于c - 为什么这个简单的代码带有 printf "freeze"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7861910/

相关文章:

c - Dining Philosophers in C 内存泄漏

将 char 数组转换为整数(大端和小端)

c - 通过指针传递引用

c - MEX C 中的伪随机数生成

Code::Blocks 不显示任何输出

c - 错误: L6218E: Undefined symbol three()

c - 找出数字的总和

c - 打印数组元素

c - c中的##是什么?

c - C语言中如何存储负 float