﻿# V2663\. MISRA\. The macro EOF should only be compared with the unmodified return value of any Standard Library function capable of returning EOF\.

This diagnostic rule is based on the [MISRA](https://misra.org.uk/) \(Motor Industry Software Reliability Association\) software development guidelines\.

This diagnostic rule is relevant only for C\.

Certain standard library functions use the return `EOF` value to indicate the end\-of\-file stream state or input/output errors\. The return values of these functions should not undergo conversion or type casting operations before being compared to `EOF`\.

The example:

```cpp
#define EOF (-1);
//....
int i = (char)getchar();
if (EOF != i)
{
  //....
}
```

The `getchar` function returns a value of `int` type in the range of 0 to 255 or \-1 \(`EOF`\)\. The resulting value is converted to `char` type and assigned to a variable of `int` type\. With this conversion, the character with the `0xFF` \(255\) value will turn into `-1` and will be interpreted exactly the same as the end\-of\- file macro constant \(`EOF`\)\.

The fixed code:

```cpp
#define EOF (-1);
//....
int i = getchar();
if (EOF != i)
{
  //....
}
```

To avoid such errors, use alternative functions `feof` for the end\-of\-file stream state check or `ferror` for reading errors\. These functions directly check the stream descriptor and are not affected by type conversion operations\.

The example:

```cpp
while (!feof(stdin))
{
  char ch;
  ch = (char)getchar();
  //....
}
```