• Compilation Errors

    What are compilation errors?

    • errors that show up when the code is getting compiled, i.e. running gcc

    Why do compilation errors occur?

    • syntax
    • missing punctations like ;
    • misspelled function names like prinf
    • wrong case like Prinf
    • wrong function arguments like too many arguments or wrong types of arguments
    • mismatched brackets like {{ }

    How to use compilation errors?

    • the error message will get you close to the line number of where the error occurred
    • the error message will give you a sense of what's wrong
    • compile often so you know exactly which part of the code you recently updated that is causing the error
    • remove half the code so you can see which half is producing the error; repeat to narrow down the bad line of code
  • Runtime Errors

    What are runtime errors?

    • errors that show up when the code is executing, i.e. after compiling and running ./hello_world

    Why do runtime errors occur?

    • simple mistake
      • wrong data types, i.e. using int instead of float
      • wrong conversion specifier
      • not taking into account the edge cases, like if it's a negative number, a really small/big number, etc
      • pointers vs variables (addresses vs values)
      • wrong variable name
    • logic
      • not accounting for a particular situation, i.e. edge case scenario like when a user enters a special character instead of a letter
      • variable gets altered in unexpected way
      • a simple mistake else where causes the error to other parts of the program
      • not allocating enough memory

    How to fix runtime errors?

    • trace through program and isolate possible areas of error
    • use printf statements to figure out where the code is dying
    • use printf statemetns to follow which logic branch is getting executed
    • use printf statements to verify the variables you are expecting
    • re-evaluate your algorithm
  • When you want to throw your computer out the window...

    • Take a deep breath, go for a jog, take a shower, or do some yoga
    • Sometimes the solution will just come to you after you take a break from it
    • If you're still lost, then delete/comment out whatever you need to get the program working, even if it's just an empty main function
    • Then add one line back at a time to see which line of code is causing the error (you don't have to be a genius to program, you have to be determined and persistent)
    • Refer back to this lecture and see if there's any debugging methods you haven't tried
    • Ask someone to look over your code with fresh eyes and see if they can catch anything you missed
  • Fix Me Example

    fix fix_me.c:

    The program is supposed to output The result is: -0.250000

  • Assignment

    Homework Problem 4.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_04_1.c
      • example:
      • zhaol_04_1.c
    • fix fix_me.c by yourself

      • make sure the program can compile, run, and calculate the correct result
      • program is supposed to output: The result is: -0.250000

    Homework Problem 4.2, at the end of this problem you should submit to Laulima:

    • a file named UHusername_04_2.c
      • example:
      • zhaol_04_2.c
    • fix fix_me_too.c

      • make sure the program can compile, run, and print out the following:
        /\/\
        'The first number is $3'
        "The second number is 5.000000?"
        /|\The special character is "\"/|\