int kr=0;
int ss =0;
while ((kr=getchar()) != EOF){
if(kr != '\n')
{
ss++;
}
printf("%d\n",ss);
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,printf正在等待,直到我按下enter然后同时打印所有顺序ss值,就像这样
.有人可以解释这种行为吗?
我有以下代码:
int main()
{
printf("hello world");
while(1);
}
Run Code Online (Sandbox Code Playgroud)
打印你好世界吗?如果是,为什么?如果不是,为什么?我在linux机器上使用gcc来编译代码.
我写了这段代码:
#include <stdio.h>
int main() {
printf("Works");
int base = 1;
do {
if (base > 1) {
for (int i = 0; i <= base; i++) {
if ((base % i) == 0) {
break;
} else {
printf("%d ", base);
}
}
}
base += 1;
} while (1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译完美,但是当我运行它时,终端刚刚关闭,我不知道如何调试它,因为我对用 c 编写程序非常陌生。
编辑:这应该永远生成素数。
我的代码如下:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("hello");
while(1){
// whatever here
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:为什么跳过第一条指令?它只运行循环,你永远不会打印.我使用gcc和g ++编译它,结果相同.