×

Loading...
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!
Ad by
  • 技多不压身,工到自然成:安省技工证书特训班,点击咨询报名!

a standard C question in an interview test

int count_digit(FILE *fp)
{
int c, cnt=0;

rewind(fp);
while (c=fgetc(fp) != EOF)
if( isdigit(c))
cnt++;

if( !feof(fp) )
cnt = -1;

return cnt;
}

The function above contains an error that prevents it from properly counting the number of digits in a file. Which one of the following describes the error?
a. The function fgetc() operates on a file descriptor not a file pointer.
b. The variable c is declared but not initialized. This is forbidden by Standard C and may have unpredictable results.
c. The inequality operator (!=) has higher precedence than assignment (=), and the loop condition is therefore incorrect.
d. There is no function rewind() that conforms to an accepted standard. The conformant function is called frewind(), and the function name has been misspelled.
e. feof() returns zero to indicate that an end-of-file condition has been encountered. The sense of the condition is therefore reversed.
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / a standard C question in an interview test
    int count_digit(FILE *fp)
    {
    int c, cnt=0;

    rewind(fp);
    while (c=fgetc(fp) != EOF)
    if( isdigit(c))
    cnt++;

    if( !feof(fp) )
    cnt = -1;

    return cnt;
    }

    The function above contains an error that prevents it from properly counting the number of digits in a file. Which one of the following describes the error?
    a. The function fgetc() operates on a file descriptor not a file pointer.
    b. The variable c is declared but not initialized. This is forbidden by Standard C and may have unpredictable results.
    c. The inequality operator (!=) has higher precedence than assignment (=), and the loop condition is therefore incorrect.
    d. There is no function rewind() that conforms to an accepted standard. The conformant function is called frewind(), and the function name has been misspelled.
    e. feof() returns zero to indicate that an end-of-file condition has been encountered. The sense of the condition is therefore reversed.
    • C
    • y, C is correct answer