×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

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