ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
printf("%d %d %d", strcmp("abc", "abd") < 0,
strcmp("ab", "abc") < 0,
strncmp("abcx", "abcy", 3) == 0);- A. 0 0 1
- B. 1 1 1
- C. 1 0 1
- D. 1 1 0
Correct answer: B. 1 1 1
strcmp compares character by character: at index 2, 'c' < 'd', so the result is negative. "ab" vs "abc": at index 2 the first string has '\0', which is smaller than 'c', so it is negative too (a prefix sorts first). strncmp looks at only the first 3 characters, which are equal, so it returns 0.
More C Programming questions
- What is printed? c int i = 3.99; int j = -3.99; int k = (int)(0.29 100); printf("%d %d %d", i, j, k);
- What is printed? c / 4-byte int with 4-byte alignment / struct S { int a; char b; } arr[5]; printf("%td %td", &arr[4] - &arr[1], (char…
- What is printed? c char s[] = "abcdef"; char p = s + 2; p[1] = 'X'; printf("%s %s %c %zu", s, p, (p - 2), strlen(p + 4));
- What is printed? c int f(int n) { if (n <= 0) return 0; return n % 2 ? f(n - 1) + n : f(n / 2) 2; } int main(void) { printf("%d %d"…
- What is printed? c unsigned char x = 0x1D, r = 0; for (int i = 0; i < 8; i++) { r = (r << 1) | (x & 1); x >>= 1; } printf("%#x", r);
- What is printed? c int a = 2, b = 3, c = 4; printf("%d %d %d", a + b c % 5 - -a, a < b < c, c > b > a);