ProgrammingHardProduct company OA
Assume ASCII. What is printed?
Assume ASCII. What is printed?
int my(const char *a, const char *b) {
while (*a && *a == *b) { a++; b++; }
return *a - *b;
}
int main(void) {
printf("%d %d %d", my("abc", "abd"), my("ab", "abc"), my("b", "a"));
}- A. -1 -100 1
- B. 0 -99 1
- C. -1 -1 1
- D. -1 -99 1
Correct answer: D. -1 -99 1
The loop stops at the first difference or at the end of a. 'c' − 'd' = −1. For "ab" vs "abc" it stops at a's '\0', and 0 − 'c' = −99. 'b' − 'a' = 1. Only the sign is guaranteed for the library strcmp, but this function returns the actual difference.
More C Programming questions
- In what order are the nodes linked after the four calls? c struct N { int v; struct N nx; }; void ins(struct N h, struct N n) { while (h…
- A function in a.c is declared static int helper(void) { ... }. Which statement is true?
- What is printed? c #define MAX(a, b) a > b ? a : b int x = 3, y = 7; printf("%d", 2 MAX(x, y));
- Assume IEEE-754 single-precision float with float arithmetic done in float. Which loops terminate? c int c1 = 0, c2 = 0; for (float f = 0…
- What is printed? c struct Shape { int w, h; int (area)(struct Shape ); }; int rect(struct Shape s) { return s->w s->h; } int tri(struct…
- What is printed? c int i = 0, s = 0; top: s += i; if (++i < 5) goto top; printf("%d %d", s, i);