shuberman
shuberman

Reputation: 1490

Why is the following C code asking input twice?

#include<stdio.h>

int main()
{
  int i, j;
  for(scanf("%d ",&i); i<=10; i++)
    printf("%d ",i);
  return 0;
}

I am a beginner in the programming world so please help me understand why on compiling the above C code it asks inputs twice.Maybe there's some logic to loop here I might be missing. Please help me understand.Thanks in advance.:)

Upvotes: 0

Views: 1672

Answers (3)

SIDDHARTHA SARKAR
SIDDHARTHA SARKAR

Reputation: 1

I was facing the same problem and the only change I did was to change "%d " to "%d". This solves the problem.

Upvotes: 0

filipivanisevic
filipivanisevic

Reputation: 3

Currencly, you placed scanf() in a for loop, which it asks for input for a 10 times.This will not happen when you remove scanf() from for loop.

Upvotes: 0

gsamaras
gsamaras

Reputation: 73366

Change this:

scanf("%d ",&i);

to this:

scanf("%d",&i);

Read more in What does space in scanf mean?

Upvotes: 4

Related Questions