当我运行这段代码时,程序卡住了,为什么?
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/