C | String | Question 9
Consider the following C program segment:
char p[20]; char *s = "string"; int length = strlen(s); int i; for (i = 0; i < length; i++) p[i] = s[length — i]; printf("%s", p); |
chevron_right
filter_none
The output of the program is? (GATE CS 2004)
(A) gnirts
(B) gnirt
(C) string
(D) no output is printed
Answer: (D)
Explanation: Let us consider below line inside the for loop
p[i] = s[length — i];
For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0′
So p[0] becomes ‘\0’. It doesn’t matter what comes in p[1], p[2]….. as P[0] will not change for i >0. Nothing is printed if we print a string with first character ‘\0′
Quiz of this Question
Recommended Posts:
- C | String | Question 3
- C | String | Question 4
- C | String | Question 8
- C | String | Question 16
- C | String | Question 11
- C | String | Question 5
- C | String | Question 15
- C | String | Question 7
- C | String | Question 6
- C | String | Question 14
- C | String | Question 2
- C | String | Question 10
- C | String | Question 12
- C | String | Question 13
- C | String | Question 1



