Reputation: 15
I've tried to refractor my very very easy programm (I'm beginner, sorry for stupidity). I wanted to put scanf() into first paragraph of loop initialization and I thought that everything would have been alright but it doesn't work as I had "predicted". The programm should get and intiger and then write following numbers till the last number would be 10 times bigger tahn the first one but instead of this it writes out numbers from 1 to the number 10 times bigger. I would be grateful for any tips and debugg. Have a nice day!
Code:
#include <stdio.h>
int main()
{
int x, i;
printf("Please put intiger:\n");
for(i = (scanf("%d", &x)); i < x + 10; i++)
printf("%d\n", i);
return 0;
}
Execution:
Please put intiger: 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Press any key to continue . . .
Upvotes: 0
Views: 91
Reputation: 121357
scanf
returns the number of values it has successfully scanned, not the value of input you have given. But what you wanted is to to print 10 numbers starting x
.
You'd need:
scanf("%d", &x);
for (i = x; i < x + 10; i++)
printf("%d\n", i);
Read the documentation of scanf
for what it returns. Also remember scanf
could fail too.
Upvotes: 1