Click on image to see enlargment

PC-lint/FlexeLint Output | Reference Manual Explanation | Home

            bug583.cpp

1    #include <stdio.h>
2
3    const int NC = 2;
4    extern char Cairo[], Tokyo[];
5    char *Cities[NC] = { Cairo, Tokyo };
6    FILE *f[NC];
7
8    int main()
9        {
10       char ch;  int i;
11       for( i = 0; i < NC; i++ )
12           {
13           f[i] = fopen(Cities[i],"wb");
14           if( !f[i] ) return 0;
15           }
16       while( (ch = getchar()) != EOF )
17           {
18           for( i = 0; i < NC; i++ )
19               putc( ch, f[i] );
20           }
21       return 0;
22       }

In order to help Santa, the elves decided to teleport toys thoughout the world. A recording of some gift is passed as standard input to this program and sent to two test cities. Why did Cairo receive only half a bicycle?


bug583.cpp lint Output

--- Module:   bug583.cpp (C++)
                         _
    while( (ch = getchar()) != EOF )
bug583.cpp(16) : Info 734: Loss of precision (assignment) (31 bits to 7 bits)
bug583.cpp(16) : Warning 583: Comparing type 'char' with EOF

Reference Manual Explanation


583    Comparing type 'Type' with EOF  -- The message is issued when
       some form of character is compared against the EOF macro.  EOF is
       normally defined to be -1.  For example:

                 while( (ch = getchar()) != EOF ) ...

       If ch is defined to be an int all is well.  If however it is
       defined to be some form of char, then trouble might ensue.  If ch
       is an unsigned char then it can never equal EOF.  If ch is a
       signed char then you could get a premature termination because
       some data character happened to be all ones.

       Note that getchar returns an int.  The reason it returns an int
       and not a char is because it must be capable of returning 257
       different values (256 different characters plus EOF, assuming an
       8-bit character).  Once this value is assigned to a char only 256
       values are then possible -- a clear loss of information.


If you have comments or questions about this bug, please post them to our   Discussion Forum


Previous Bug - Bug #464 - November 2008

Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2009, Gimpel Software, All rights reserved.