ProgrammingVery hardProduct company OA · GATE CS
Which statement about this C declaration is true?
Which statement about this C declaration is true?
char a[5] = "hello";
- A. a holds "hell" followed by '\0'
- B. It fails to compile because "hello" needs 6 bytes
- C. It compiles, but a holds no terminating '\0', so passing it to strlen is undefined
- D. The compiler silently enlarges a to 6 bytes
Correct answer: C. It compiles, but a holds no terminating '\0', so passing it to strlen is undefined
C has a special rule: if the array has exactly room for the characters but not the '\0', the terminator is simply dropped. a is 5 chars, not a string, so string functions would run off its end. (C++ rejects this line.)
More C Programming questions
- What is printed? c #define CUBE(x) ((x) (x) (x)) int n = 0; int f(void) { n++; return 2; } int main(void) { int r = CUBE(f()); printf("%d…
- What is printed? c unsigned char rotl(unsigned char v, int k) { return (unsigned char)(v << k | v >> (8 - k)); } int main(void) {…
- With int a[3], p = a; which statement fails to compile?
- Assume 4-byte int. What is printed? c struct Msg { int len; char data[]; }; struct Msg m = malloc(sizeof m + 10); printf("%zu", sizeof m);
- What is printed? c printf("%zu %zu", sizeof("ab" "cd"), strlen("ab0cd"));
- What is printed? c int n = printf("%d", printf("%s", "abc")); printf(" %d", n);