Pritesh Patel
Pritesh Patel

Reputation: 119

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"

Upvotes: 1

Views: 73

Answers (3)

Gaurav Mamgain
Gaurav Mamgain

Reputation: 1

  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

Upvotes: 0

jpw
jpw

Reputation: 44871

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.

Upvotes: 1

Dr.Jukka
Dr.Jukka

Reputation: 2396

I would try writing the for loop as:

for(i=1;i < 6;i++) { printf(“%d”,i); }

Upvotes: 0

Related Questions