Reputation: 119
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
Reputation: 1
Upvotes: 0
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
Reputation: 2396
I would try writing the for loop as:
for(i=1;i < 6;i++) { printf(“%d”,i); }
Upvotes: 0