Programming Fundamentals

1511 Initial Debugging Guide

First, figure out what kind of error do you have?

Compiling Errors

  1. Start from the very top, look for where you wrote dcc my_code.c -o my_code, you can ignore every error except the first one for now!

  2. Read the whole error message! If some of it doesn’t make sense, that’s alright, usually the “dcc explanation” part will explain the error in English.

  3. Look at the line number and character number given, have a look at this line of code in your file and see if you can find your bug with the error message in mind.

    compiler_error
  4. If you think you’ve fixed the bug, now save your file, recompile and start from step 1 again.

Need more help?

Try running dcc-help to get further explaination on yoru error message.

Autotest Errors

  1. If you have multiple tests failing, pick one of the test that is failing to concentrate on, this could be any of them (but something near the top is probably simpler!). So look for the line that says “failed”, like this:

    test_number
  2. Read through the whole test, and look out for the part that shows the different between your output and the correct output.

    • The red, (anything after the “-” ) is your output, and the green (anything after the “+”) is the expected output:

      autotest_differences

      In this example, the autotest expected “Please enter the number of students and tutors: 5 + 5 = 10” but your program outputted “Enter the number of students and tutors: 5 + 5 = 10

  3. You could be getting a runtime error, this might look like this:

    autotest_runtime_error

If so, then manually run the test case that you were looking at without the autotest, and follow the instructions for runtime errors (below).

  1. If you are still confused, then manually run the test case that you were looking at without the autotest. Some things to look out for when doing this are:

    • Are the new lines in the correct spot (compare against the example given in the question description)

    • Is the answer actually the correct answer? Figure out the answer on pen and paper and compare with the autotest output to check.

    • Check that you are typing in the exact same input. Sometimes there is no “\n” at the end of the input, this means that to run the test, you might need to type [ctrl-d] instead of [enter] then [ctrl-d]. Like in the following example:

      missing_newline
  2. Fix the error in the output in your program. Now save your file, rerun the autotest and start from step 1 again.

Runtime Errors

Usually runtime errors mean that you have one of the following errors. Try to figure out which one you have.

Some way to fix this are:

  1. Making sure that you have no warnings after compiling with dcc. Warnings don’t stop your code from compiling, but they can cause runtime errors when you run the program after compilation. Try not to get into the habit of ignoring warnings.

    dcc_warning
  2. Read the whole error, dcc will point out a line of code and the values of some variables. Check what variables that line of code uses and if any of them are uninitialized.

  3. If some values are not what you expect them to be, then you can either

    • Think about what you want the values to be, then hand execute your code with pen & paper to see if your expectations and code line up.
    • Put in various print statements like the ones below and check that it lines up with your hand execution.
    printf("variable1: %d, variable2: %d\n", variable1, variable2);
    

Logic Error

Logic errors are the hardest to figure out, as there are no error messages to help you. Some steps to help find them are: