ProgrammingVery hardGATE CS
What is printed? Assume pointers are 8 bytes and the string literal's characters are those between the quotes.
What is printed? Assume pointers are 8 bytes and the string literal's characters are those between the quotes.
#include <stdio.h>
#include <string.h>
int main(void) {
char s[] = "ab\0cd";
char *p = "ab\0cd";
printf("%zu %zu %zu %zu", sizeof(s), strlen(s), sizeof(p), strlen(p + 3));
return 0;
}- A. 5 2 8 2
- B. 6 2 5 2
- C. 6 5 8 2
- D. 6 2 8 2
Correct answer: D. 6 2 8 2
The array s holds a, b, \0, c, d and the final terminator: sizeof(s) = 6, while strlen stops at the first \0 and gives 2. sizeof(p) is the pointer size (8), and p + 3 points to "cd", so strlen is 2.
More C Programming questions
- What is the exact output? c #include <stdio.h> int main(void) { int n = printf("%d%d", 12, 345); printf("|%d", n); return 0; }
- What is printed? Assume int is 4 bytes and pointers are 8 bytes. c #include <stdio.h> void f(int a[10]) { printf("%zu ", sizeof a); } int…
- What is printed? Assume the usual ABI: char 1 byte, short 2, int 4, with each member aligned to its own size and the struct padded to a…
- What is printed? c #include <stdio.h> int main(void) { char s[] = "abcd"; char p = s; (p)++; p++ += 2; ++p; p[1]--; printf("%s", s)…
- What is printed? Assume C99 or later (division truncates towards zero). c #include <stdio.h> int main(void) { printf("%d %d %d %d", -17 /…
- What is printed? Assume unsigned int is 32 bits and unsigned char is 8 bits. c #include <stdio.h> int main(void) { unsigned int x = 1u <<…