简体   繁体   中英

Confusion about output of program

I am new to C programming and I am currently learning loops. 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"

You need to include the <stdio.h> header, and also, main needs a return type (int) and a return value. Changing the program to this will make it compile (at least it did using GCC) and run:

#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" .

Apart from that, doing the printf inside the loop head might be legal, but it's pretty bad style. 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循环编写为:

for(i=1;i < 6;i++) { printf(“%d”,i); }
  1. I have run this program manually on my notebook and i got Output 23456
  2. Then i run this on Dev c++ and it is giving the same output 23456 without any error and i have just copied and pasted from ur question dun know why its showing error on ur runtime may be u have not saved it as C file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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