Behold the following program in C:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char s[20];
int i;
for (i = 0; i < 15; i++) s[i] = '0' + i % 10;
s[i] = '\0';
printf("%s\n", s); /* First line*/
for (i = 0; i < 15; i += 4) s[i] = '\0';
for (i = 0; i < 15; i += 3) {
printf("%d-%s\n", i, s+i); /* Lines two through six */
}
strcat(s+7, s+13);
printf("%s\n", s+7); /* Last line */
return 0;
}
This program has seven lines of output. Each clicker question asks for a line of output.
Remember, it’s strcat(destination, source)
.
Question 1: What is line 1?
s: 01234567891234*Let's label where s+11 is:
s: 01234567891234* | sThat lets us answer the first question: "1234".
Question 2: What is line 2?
s: *123*567*901*34*And let's label the pointers at s is:
s: *123*567*901*34* | 0That lets us answer the second question: 0-
Question 3: What is line 3?
s: *123*567*901*34*And let's label the pointers at s+3 is:
s: *123*567*901*34* | | 0 3That lets us answer the third question: 3-3
Question 4: What is line 4?
s: *123*567*901*34*And let's label the pointers at s+6 is:
s: *123*567*901*34* | | | 0 3 6That lets us answer the fourth question: 6-67
Question 5: What is line 5?
s: *123*567*901*34*And let's label the pointers at s+9 is:
s: *123*567*901*34* | | | | 0 3 6 9That lets us answer the fifth question: 9-901
Question 6: What is line 6?
s: *123*567*901*34*And let's label the pointers at s+12 is:
s: *123*567*901*34* | | | | | 0 3 6 9 12That lets us answer the sixth question: 12-
Question 7: What is line 7?
s: *123*567*901*34* | | s+7 s+13The strcat() will find the first null character after (s+7). That's at s+8. It then copies the characters from s+13 to the null character, and null terminates. The string now looks like:
s: *123*56734*1*34* | | s+7 s+13When we print starting at s+6, we get the characters "6734".