• C

array initilization.

#include <stdio.h>

int main(void) {

int i=0;
int numbers[20];

while(scanf("%d",&numbers[i])!=EOF) i++;

return 0;


// what's wrong in this code...
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What error are u facing...........????

Syntax wise, your code has no errors. But it has few logical errors; the while loop in your code should have an extra condition to check for array boundry so that the index 'i' does not go beyond the aray limits, this could be done this way:

      while(i < 20 && scanf("%d", &numbers[i]) != EOF) i++;

I added a check on the value of 'i' so that 'i' will not go beyond the array maximum index which is '19' in your case.
Also, the check on the index is placed before the scanf() function so that it is executed first.

Khalid Omar

scanf  function returns the number of input items assigned
when you try to give EOF, it is assigned to numbers[i] and 1 is returned !! which is not equal to EOF

perhaps you want this

do {
} while (numbers[i-1] != EOF);
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Kent OlsenDBACommented:

Hi Sunnycoder,

I don't think that that is the answer, either.  scanf() returns EOF, it doesn't store EOF as a data value.

The original code should work if the input source is a data file and NOT the keyboard.  This should work fine:

program.exe < DataFile

gets() returns EOF if a lone carriage return is entered from the keyboard.  (Don't use gets()!)  scanf() does not return EOF when input is from the keyboard so other trappings need to occur.

Revised verion of the original code that should not give errors is :

int main(void)
int i=0;
int numbers[20];
while(scanf("%d",&numbers[i]) && i < 20 && numbers[i]!=EOF ) i++;
return 0;

you are right Kent... I shud have read the man page completely

using fgets() might help
Kent OlsenDBACommented:

Hi Sunnycoder,

fgets() doesn't return EOF from the keyboard, either.  You'd still have to test for a line-length of zero, which would also mean that blank lines are no longer allowed.

Hi Ajar,

The test "numbers[i]!=EOF" is the same thing that Sunnycoder tripped over.  scanf() won't store EOF at numbers[i] when EOF is encountered.  Since EOF is generally defined to be -1, your loop will exit if scanf() stores a -1.

There needs to be two test cases.  One for keyboard input and one for file input.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.