Question: My question is not about algorithm, but something confusing about C programming language.
I met a problem as below:

**Problem description**

N! (N factorial) can be quite irritating and difficult to compute for large values of N. So instead of calculating N!, I want to know how many digits are in it. (Remember that N! = N \* (N - 1) \* (N - 2) \* ... \* 2 \* 1) 

**Input**

Each line of the input will have a single integer N on it 0 < N < 1000000 (1 million). Input is terminated by end of file. 

**Output**

For each value of N, print out how many digits are in N!. 

My code is like that:

```
 #include<stdio.h>
 int main()
{
    int n ;
    while(scanf("%d",&n)&&n!=EOF)//1
    //while(scanf("%d",&n)!=EOF)//2
    {
        int result=1;
        int i;
        double temp = 1;
        for(i=n;i>0;i--)
        {
            temp*=i;
            while((temp/10)>1)
            {
                result++;
                temp=temp/10;
            }           
        }
        printf("%d\n",result);
    }
    return 0;
}

```

When I used `while(scanf("%d",&n) && n!=EOF)`, the Online judge I was using showed that `Time Limit Exceeded`,when I changed it to `while(scanf("%d",&n)!=EOF)`, every thing goes well.I think `n!=EOF` may take too much time, So what is the essence of `EOF` ?

Answer: 
It is not the time of the 2 sentences, is that they have different meaning

The next one compares the value read by scanf with EOF, this is an error, scanf will not load any value in n if it reaches end of file:

```
while(scanf("%d",&n) && n!=EOF)

```

This one is the correct way to detect end of file:

```
while(scanf("%d",&n)!=EOF)

```

Generally EOF == -1, as the input in the judge doesn't contains -1 your program loops indefinitely.

As stated in the comments checking how many conversions scanf did is better in general, as it checks if the input is more or less valid(\*). Given you asked 1 conversion in the format string ("%d") you can check if scanf returns 1:

```
while(scanf("%d", &n) == 1)

```

(\*) It may fail to verify some cases if you have to follow a strict format (for example if the number is required to be followed by exaclty one \n), it would be even better do the conversions with strtol, checking that strtol consumes the complete line.

As this particular problem is from a programming contest using an online judge the input is well formed and such tests are usually not necessary.
Neither condition is proper one to use (but the one with `scanf("%d", &n) != EOF` is the better one to use).

The loop should be written as:

```
while (scanf("%d",&n) == 1)

```

because you're asking `scanf` to convert 1 value, and the return value from `scanf` is the number of items successfully converted.