几道考试题,不得其解,望大虾指导
1.Given an input file containing a single line of integer values separated by spaces, which ofthe following code fragments computes the sum of all the numbers? Assume that the file
pointer in is set up correctly; num and sum are declared as int variables with an initial value
of 0.
(i) while (fscanf(in, "%i", &num) > 0)
sum += num;
(ii) while (fscanf(in, "%i", &num) != 0)
sum += num;
(iii) while (fscanf(in, "%i", &num) >= 0)
sum += num;
(A) None of (i), (ii), (iii).
(B) (i) only.
(C) (ii) only.
(D) (i) and (iii) only.
(E) (ii) and (iii) only.
这道题,我着实没想通返回值的问题,不应该是返回NULL么,,,感觉选1和2但是又没有这个选项,,,
11. The following skeleton code counts the number of uppercase letters in an input string.
char c;
int count = 0;
while ((c = getchar()) != ’\n’)
{
/* Code to count the number of uppercase letters. */
}
As an example, if the input string is:
The quick brown FOX jumps over the lazy DOG.
the value of count is 7. Which of the following are valid statements to count the number of
uppercase letters?
(i) count += isupper(c);
(ii) count += !!isupper(c);
(iii) count += isupper(c) ? 1 : 0;
(iv) count += islower(c) ? 0 : 1;
(A) (i) only.
(B) (iii) only.
(C) (i) and (iii) only.
(D) (ii) and (iii) only.
(E) (iii) and (iv) only.
小弟这道题觉得四个好像都对啊,,,迷茫,,,
Given the following C code fragment (line numbers are indicated):
1: double x, z;
2: int y;
3: scanf("%d %f", &y, &x);
4: z=x+y;
5: printf("x+y=z\n", z);
What do we need to do to fix this code so that it works correctly?
(i) Change line 3 to scanf("%f %d", &y, &x);
(ii) Change line 3 to scanf("%d %lf", &y, &x);
(iii) Change line 2 to double y;
(iv) Change line 4 to z=(x+y);
(v) Change line 5 to printf("x+y=%f", z);
(A) Changes (i) and (iii) are necessary.
(B) Changes (ii), (iii), and (v) are necessary.
(C) Changes (ii) and (v) are necessary.
(D) Changes (iv) and (v) are necessary.
(E) None of the options (A) to (D) are correct.
这题感觉是C,,,但为啥double 用%f 输出呢?
小弟刚学C,,,不才,,,望各位指点一下,,,