ProgrammingModerateTCS NQT · Wipro
What is printed?
What is printed?
char s[8] = {'a', 'b'};
printf("%zu %zu %d", strlen(s), sizeof s, s[7]);- A. 2 8 0
- B. 2 2 0
- C. 2 8 garbage
- D. 8 8 0
Correct answer: A. 2 8 0
When an array has an initialiser, the elements not listed are set to 0, so s[2] to s[7] are '\0'. That makes s a valid string of length 2, sizeof s is the declared 8, and s[7] is 0.
More C Programming questions
- What does r(3) print, and how many digits would r(6) print? c void r(int n) { if (n == 0) return; r(n - 1); printf("%d", n); r(n - 1); }
- What is printed? c const char s = "abcdefgh"; const char p = s + 2, q = strchr(s, 'g'); printf("%td %c %s", q - p, (p + (q - p) / 2), q);
- Given int f(int); and int (fp)(int) = f; which of these calls are valid? (i) fp(2) (ii) (fp)(2) (iii) (fp)(2) (iv) (&fp)(2)
- What is printed? c union U { int i; float f; } u = {3.9}; printf("%d", u.i);
- What is printed? c int i = 0, c = 0; while (i < 10 && ++c && i++ % 3 != 2); printf("%d %d", i, c);
- Given this allocation, which code frees everything without leaking or touching freed memory? c int m = malloc(3 sizeof m); for (int i = 0…