UMBC CMSC 201 Fall '00 CSEE | 201 | 201 F'00 | lectures | news | help

Errors Lab 1

Have the students type the following file in using xemacs. If the students have copied the .emacs file into their accounts they will see that the indentation and placement of braces will be done automatically. This is because the xemacs text editor knows the format of c files. Any file they make that ends in .c will be treated as a c source file by the editor.

Students may note that occasionally the indentation appears to be wrong. This is because they have previously made some syntax error, like forgetting to close a comment, parenthesis, or quote. That is why we are typing in the correct version, so that we won't have to fight with every line for indentaion. Then we'll go back and make small changes to produce errors.

/* * Filename: correct.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains good code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); printf ("Please enter the second integer: "); scanf ("%d", &num2); /* Calculate the average */ average = (num1 + num2)/2.0; /* Print the average to 3 decimal places */ printf ("The average of %d and %d is %.3f\n", num1, num2, average); }

After typing in the correct version, have the students change their file by making the changes in the following list:

  1. Change the */ at the end of the file header comment to a *\
  2. Remove the semicolon from the end of the first printf statement
  3. Change the word printf to print in the prompt for the second int
  4. Remove the & from in front of num2 in the second scanf statement
  5. Capitalize Average in the calculation statement
  6. Change 2.0 to 2 in the calculation statement
  7. Remove the semicolon from the end of the calculation statement
  8. Change the 2 %d's in the printing of the results to %f's
  9. Change the %.3f in the printing of the results to %3f
  10. Change num2 in the printing of the results to num

producing the following:

/* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. *\ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n") printf ("Please enter the first integer: "); scanf ("%d", &num1); print ("Please enter the second integer: "); scanf ("%d", num2); /* Calculate the average */ Average = (num1 + num2)/2 /* Print the average to 3 decimal places */ printf ("The average of %f and %f is %3f\n", num1, num, average); } Next have the students compile the file. It should produce the following error messages. irix1[73]% cc errors.c "errors.c", line 23: warning(1009): nested comment is not allowed /* Print the greeting and prompt the user for 2 ints */ ^ "errors.c", line 24: error(1079): expected a type specifier printf ("This program finds the average of two integers.\n") ^ "errors.c", line 24: error(1137): unnamed prototyped parameters not allowed when body is present printf ("This program finds the average of two integers.\n") ^ "errors.c", line 25: error(1129): expected a "{" printf ("Please enter the first integer: "); ^ "errors.c", line 35: warning(1012): parsing restarts here after previous syntax error } ^ "errors.c", line 35: warning(1116): non-void function "printf" (declared at line 24) should return a value } ^ unable to proceed because of earlier errors 3 errors detected in the compilation of "errors.c". irix1[74]% Examining the first error message, we notice that the compiler thinks we are nesting comments, meaning that we have one comment within another one. Since this message is being produced on the line that contains the first comment that is within the code, we must not have ended the previous comment. In this case, the file header comment.

Have the students fix this by ending the file header comment properly with a */

Fix only this one error, producing the following code:

/* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n") printf ("Please enter the first integer: "); scanf ("%d", &num1); print ("Please enter the second integer: "); scanf ("%d", num2); /* Calculate the average */ Average = (num1 + num2)/2 /* Print the average to 3 decimal places */ printf ("The average of %f and %f is %3f\n", num1, num, average); } Then compile again. irix1[73]% cc errors.c "errors.c", line 25: error(1065): expected a ";" printf ("Please enter the first integer: "); ^ "errors.c", line 28: warning(1178): argument is incompatible with corresponding format string conversion scanf ("%d", num2); ^ "errors.c", line 28: warning(1551): variable "num2" is used before its value is set scanf ("%d", num2); ^ "errors.c", line 31: error(1020): identifier "Average" is undefined Average = (num1 + num2)/2 ^ "errors.c", line 34: error(1065): expected a ";" printf ("The average of %f and %f is %3f\n", num1, num, average); ^ 3 errors detected in the compilation of "errors.c". irix1[74]% Now the first error message states that you have a missing semicolon, but it shows you a line that looks fine and has a semicolon. Notice though that the little arrow is pointing to the very beginning of the line. This is where the compiler has discovered the error, meaning that the previous statement is missing its semicolon.

Fix this error producing the following file:

/* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); print ("Please enter the second integer: "); scanf ("%d", num2); /* Calculate the average */ Average = (num1 + num2)/2 /* Print the average to 3 decimal places */ printf ("The average of %f and %f is %3f\n", num1, num, average); } Now compile again. The following messages are produced. The first two messages are warnings, not errors. Since warnings do not keep a file from compiling, let's skip the warnings for now and just work on the errors. The next error message states that the variable named Average is undefined. This is the type of message you'll get when you have misspelled a variable name. irix1[73]% cc errors.c "errors.c", line 28: warning(1178): argument is incompatible with corresponding format string conversion scanf ("%d", num2); ^ "errors.c", line 28: warning(1551): variable "num2" is used before its value is set scanf ("%d", num2); ^ "errors.c", line 31: error(1020): identifier "Average" is undefined Average = (num1 + num2)/2 ^ "errors.c", line 34: error(1065): expected a ";" printf ("The average of %f and %f is %3f\n", num1, num, average); ^ 2 errors detected in the compilation of "errors.c". irix1[74]% Fix the spelling of average producing the following code. /* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); print ("Please enter the second integer: "); scanf ("%d", num2); /* Calculate the average */ average = (num1 + num2)/2 /* Print the average to 3 decimal places */ printf ("The average of %f and %f is %3f\n", num1, num, average); } Let's try compiling again. irix1[73]% cc errors.c "errors.c", line 28: warning(1178): argument is incompatible with corresponding format string conversion scanf ("%d", num2); ^ "errors.c", line 28: warning(1551): variable "num2" is used before its value is set scanf ("%d", num2); ^ "errors.c", line 34: error(1065): expected a ";" printf ("The average of %f and %f is %3f\n", num1, num, average); ^ 1 error detected in the compilation of "errors.c". irix1[74]% We have another missing semicolon. Fix it producing the following: /* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); print ("Please enter the second integer: "); scanf ("%d", num2); /* Calculate the average */ average = (num1 + num2)/2; /* Print the average to 3 decimal places */ printf ("The average of %f and %f is %3f\n", num1, num, average); } Now let's compile again. irix1[73]% cc errors.c "errors.c", line 28: warning(1178): argument is incompatible with corresponding format string conversion scanf ("%d", num2); ^ "errors.c", line 28: warning(1551): variable "num2" is used before its value is set scanf ("%d", num2); ^ "errors.c", line 34: warning(1178): argument is incompatible with corresponding format string conversion printf ("The average of %f and %f is %3f\n", num1, num, average); ^ "errors.c", line 34: error(1020): identifier "num" is undefined printf ("The average of %f and %f is %3f\n", num1, num, average); ^ 1 error detected in the compilation of "errors.c". irix1[74]% Notice that a new error message is produced. It's showing us that we have used num instead of num2 in the printing of the results. Fix it, producing the following: /* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); print ("Please enter the second integer: "); scanf ("%d", num2); /* Calculate the average */ average = (num1 + num2)/2; /* Print the average to 3 decimal places */ printf ("The average of %f and %f is %3f\n", num1, num2, average); } When we compile this time, we have just warnings and a new kind of error message. The error message shown is a linker error message. Linker errors commonly happen when you have misspelled the name of a function that you are calling, that is in some other file or one of the standard libraries. Here we have print instead of printf. These errors are usually harder to find since no line numbers are given. The linker can't give line numbers, since it is working with object code, not source code. irix1[73]% cc errors.c "errors.c", line 28: warning(1178): argument is incompatible with corresponding format string conversion scanf ("%d", num2); ^ "errors.c", line 28: warning(1551): variable "num2" is used before its value is set scanf ("%d", num2); ^ "errors.c", line 34: warning(1178): argument is incompatible with corresponding format string conversion printf ("The average of %f and %f is %3f\n", num1, num2, average); ^ "errors.c", line 34: warning(1178): argument is incompatible with corresponding format string conversion printf ("The average of %f and %f is %3f\n", num1, num2, average); ^ ld32: ERROR 33: Unresolved text symbol "print" -- 1st referenced by errors.o. Use linker option -v to see when and which objects, archives and dsos are loaded. ld32: INFO 152: Output file removed because of error. irix1[74]% Find the print and replace it with printf, producing the following: /* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); printf ("Please enter the second integer: "); scanf ("%d", num2); /* Calculate the average */ average = (num1 + num2)/2; /* Print the average to 3 decimal places */ printf ("The average of %f and %f is %3f\n", num1, num2, average); } Now let's compile again. irix1[73]% cc errors.c "errors.c", line 28: warning(1178): argument is incompatible with corresponding format string conversion scanf ("%d", num2); ^ "errors.c", line 28: warning(1551): variable "num2" is used before its value is set scanf ("%d", num2); ^ "errors.c", line 34: warning(1178): argument is incompatible with corresponding format string conversion printf ("The average of %f and %f is %3f\n", num1, num2, average); ^ "errors.c", line 34: warning(1178): argument is incompatible with corresponding format string conversion printf ("The average of %f and %f is %3f\n", num1, num2, average); ^ Since the only messages are warnings, this compile worked and an executable file called a.out was produced. Let's try to run the program and see what happens. irix1[74]% a.out This program finds the average of two integers. Please enter the first integer: 2 Please enter the second integer: 5 Segmentation fault (core dumped) irix1[75]% Oh, Boy. We're in trouble now. Actually, the segmentation fault, a run-time error, is produced because we have forgotten the & in front of num2 in the second scanf statement. At this point in the semester, this is the most likely reason that you'd get a seg fault. This same missing semicolon is also causing the first two warning messages. Let's fix this now, producing the following: /* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); printf ("Please enter the second integer: "); scanf ("%d", &num2); /* Calculate the average */ average = (num1 + num2)/2; /* Print the average to 3 decimal places */ printf ("The average of %f and %f is %3f\n", num1, num2, average); } Now compiling again we see just two warnings left. irix1[73]% cc errors.c "errors.c", line 34: warning(1178): argument is incompatible with corresponding format string conversion printf ("The average of %f and %f is %3f\n", num1, num2, average); ^ "errors.c", line 34: warning(1178): argument is incompatible with corresponding format string conversion printf ("The average of %f and %f is %3f\n", num1, num2, average); ^ Let's just ignore them and try running the new executable that was produced, a.out irix1[74]% a.out This program finds the average of two integers. Please enter the first integer: 2 Please enter the second integer: 5 The average of 0.000000 and 0.000000 is 3.000000 irix1[75]% Great :-( We entered 2 and 5 and it prints 0.000000 and 0.000000 !!! That's because we're trying to print ints with %f. That never works well. Let's change the first two %f's to %d's producing the following: /* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); printf ("Please enter the second integer: "); scanf ("%d", &num2); /* Calculate the average */ average = (num1 + num2)/2; /* Print the average to 3 decimal places */ printf ("The average of %d and %d is %3f\n", num1, num2, average); } Let's compile again. irix1[73]% cc errors.c irix1[74]% Yeeee Haaaa !!!! :-) :-) :-)
Now let's run this thing. irix1[74]% a.out This program finds the average of two integers. Please enter the first integer: 2 Please enter the second integer: 5 The average of 2 and 5 is 3.000000 irix1[75]% No, it was supposed to be to three decimal places, not 6. Fix %3f to be %.3f, producing the following: /* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); printf ("Please enter the second integer: "); scanf ("%d", &num2); /* Calculate the average */ average = (num1 + num2)/2; /* Print the average to 3 decimal places */ printf ("The average of %d and %d is %.3f\n", num1, num2, average); } Okay, now let's compile again. irix1[73]% cc errors.c irix1[74]% Well, at least we didn't introduce any new errors. Let's try to run it again. irix1[74]% a.out This program finds the average of two integers. Please enter the first integer: 2 Please enter the second integer: 5 The average of 2 and 5 is 3.000 irix1[75]% Okay, but the average of 2 and 5 should be 3.500. We must be doing integer division. Let's fix this by changing the 2 to 2.0 in the calculation, producing the following /* * Filename: errors.c * Author: Sue Bogar * Date written: 9/18/99 * SSN: 123-45-6789 * Section #: 101 * Email: bogar@cs.umbc.edu * * This file contains buggy code and is to be used as an exercise in the * CMSC 201 extra lab session # 1. * Errors are to be found and fixed one at a time, just as you would fix * your errors in a real project. */ #include <stdio.h> main ( ) { int num1, num2; float average; /* Print the greeting and prompt the user for 2 ints */ printf ("This program finds the average of two integers.\n"); printf ("Please enter the first integer: "); scanf ("%d", &num1); printf ("Please enter the second integer: "); scanf ("%d", &num2); /* Calculate the average */ average = (num1 + num2)/2.0; /* Print the average to 3 decimal places */ printf ("The average of %d and %d is %.3f\n", num1, num2, average); } And recompile irix1[73]% cc errors.c irix1[74]% And run the program irix1[74]% a.out This program finds the average of two integers. Please enter the first integer: 2 Please enter the second integer: 5 The average of 2 and 5 is 3.500 irix1[75]% Success !!! :-) :-) :-)


CSEE | 201 | 201 F'00 | lectures | news | help