ProgrammingHardInfosys · Capgemini
What is printed?
What is printed?
char s[10] = "abc";
char t[] = "xyz";
strncat(s, t, 2);
printf("%s %zu %d", s, strlen(s), strncmp(s, "abcx", 4) == 0);- A. abcxyz 6 1
- B. abcxy 5 0
- C. abcxy 5 1
- D. abcx 4 1
Correct answer: C. abcxy 5 1
strncat appends at most 2 characters of t and always adds a '\0', giving "abcxy" (length 5). strncmp compares only the first 4 characters, "abcx" against "abcx", so it returns 0 and the test prints 1.
More C Programming questions
- What is printed? c char s[] = {"alpha", "beta", "gamma", "delta"}; char p = s; p += 2; printf("%c ", p); printf("%c ", ((p - 1) + 2))…
- What is printed? c int c = 0; int g(int n) { c++; if (n <= 2) return 1; return g(n - 1) + g(n - 2) + g(n - 3); } int main(void) { int r =…
- Assume 32-bit unsigned int. What is printed? c unsigned f(unsigned x) { return (x & 0x55555555u) << 1 | (x >> 1) & 0x55555555u; }…
- What is printed? c #define X 3 #define Y X 2 #if Y == 6 #define Z 1 #else #define Z 2 #endif printf("%d %d", Z, Y + 1 2);
- What is printed? c #define SHOW(x) printf(#x " = %d", x) int a = 3; SHOW(a 2 + 1);
- file1.c contains static int count = 5; at file scope. file2.c contains extern int count; and uses count in main. Nothing else defines…