×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

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