ProgrammingVery hardProduct company OA
The user types abcdefghij and presses Enter. What is strlen(buf) after `char buf[8]; fgets(buf, sizeof buf, stdin);`?
- A. 7
- B. 8
- C. 10
- D. 11
Correct answer: A. 7
fgets stores at most n − 1 = 7 characters and then the '\0', so buf holds "abcdefg". The rest of the line, including the newline, stays in the input for the next read. It never overflows the buffer, unlike gets.
More C Programming questions
- After scanf("%d", &n); reads the input 42 followed by Enter, why does the next getchar() return 'n' instead of waiting for new input?
- What is printed? c int buf[4], head = 0, count = 0; for (int v = 1; v <= 6; v++) { buf[(head + count) % 4] = v; if (count < 4) count++…
- What is printed? c int st[10], top = -1; #define PUSH(x) (st[++top] = (x)) #define POP() (st[top--]) int main(void) { PUSH(3); PUSH(5)…
- A binary tree is stored with child indices (−1 for none). What does h(0) return? c int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1}…
- With the tree below, what does in(0) print? c char key[] = "ABCDE"; int L[] = {1, 3, -1, -1, -1}, R[] = {2, 4, -1, -1, -1}; void in(int n)…
- What is wrong with this code? c char s = malloc(5); strcpy(s, "hello");