[英]Confusion about output of program
I am new to C programming and I am currently learning loops. 我是C编程新手,目前正在学习循环。 In the below program,
在下面的程序中
#include<stdio.h>
main()
{
int i;
for(i=1;i++<=5;printf("%d",i));
}
i tried to compile in dev c++ compiler but it is giving error "[Error] ld returned 1 exit status" 我试图在dev c ++编译器中进行编译,但给出错误“ [Error] ld返回了1个退出状态”
You need to include the <stdio.h>
header, and also, main needs a return type (int) and a return value. 您需要包括
<stdio.h>
标头,并且main还需要一个返回类型(int)和一个返回值。 Changing the program to this will make it compile (at least it did using GCC) and run: 将程序更改为此将使其编译(至少使用GCC进行过编译)并运行:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i=1;i++<=5;printf("%d",i));
return 0;
}
The quotes you used in the “%d”
are illegal too, use normal quotes: "%d"
. 您在
“%d”
中使用的引号也是非法的,请使用普通引号: "%d"
。
Apart from that, doing the printf
inside the loop head might be legal, but it's pretty bad style. 除此之外,在循环头内执行
printf
可能是合法的,但这是非常糟糕的样式。 Usually in a for-loop you would have have initialization;condition;increment(or decrement or w/e) in the head, and do side-effects in the body of the statement. 通常在for循环中,您将在头中具有初始化; condition; increment(或减量或w / e) ,并在语句主体中产生副作用。
我会尝试将for循环编写为:
for(i=1;i < 6;i++) { printf(“%d”,i); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.