In spite of the efforts of language designers, there is still a lot of C code out there. C is its own language and has its own set of problems. Here are a few unique and special mistakes that only a C programmer can make.
This program is supposed to combine your first and last names and print them.
A sample run should look like:
First: John
Last: Smith
Hello: John Smith
Thank you for using Acme Software.
But what does the program really do?
1 /************************************************
2 * Greetings -- Ask the user for his first *
3 * name and his last name. *
4 * Then issue a greeting. *
5 ************************************************/
6 #include <stdio.h>
7 #include <string.h>
8 int main()
9 {
10 char first[100]; /* The first name */
11 char last[100]; /* The last name */
12 char full_name[201];/* The full name */
13
14 /* Get the first name */
15 printf("First: ");
16 fgets(first, sizeof(first), stdin);
17
18 /* Get the last name */
19 printf("Last: ");
20 fgets(last, sizeof(last), stdin);
21
22 /* Make full_name = "<first> <last>" */
23 strcpy(full_name, first);
24 strcat(full_name, " ");
25 strcat(full_name, last);
26
27 /* Greet the user by name */
28 printf("Hello %s\n", full_name);
29 printf("Thank you for "
30 "using Acme Software.\n");
31 return (0);
32 }