From the course: Debugging C Code
Catching bugs - C Tutorial
From the course: Debugging C Code
Catching bugs
- [Instructor] Here's the source code for exercise file 0102 editor 1.C. Can you spot the error? It's obvious in this editor as it is in many other editors. In fact, Codespaces presents the tab titled in pink. The problems tab shows three potential issues, and you see a squiggly red underline in the code. Comment to many editors, however, is color coding, which is a key clue. You see orange text bleeding out from a missing double quote here. When I add it, the editor updates the text, color coding is restored and the bug is caught. This type of bug catching is only possible when an editor has C language syntax checking, as does the editor here in Codespaces. Source code editors are excellent at matching items such as single and double quotes, parentheses, braces and brackets, anything dealing with a pair of required items. The parentheses is matched. I only typed the left parentheses, and a hint appears right here offering suggestions on how to fix what might be missing. The brace is matched. When I press enter, a blank line is inserted and indented. Here's the print F function's format and description. Double quotes are matched. Like many editors that match pairs, you can type the pair at the end, and it doesn't double up. Double quote and parentheses. The red squiggle here is the editor reminding me of something that's missing. What could be missing? Well, it's the semicolon at the end of this statement. And the warning vanishes. Use these editor hints to avoid bugs before you build. This code has some problems, but I want to show you how they're displayed by the build process at the command prompt in the terminal tab. First, I'm going to change to the source code directory here in Codespaces. I need to change to the chapter directory, chapter one, and then the video directory, 0102, and here are the files. I'm using the clang compiler and I'm adding the wall switch -Wall. This activates all warnings and the file name. Upon building you see a warning and two errors. Line seven has an issue. The issue is with a matching conversion character. Line seven also appears to be missing a semicolon, and line nine reports a missing value from the return statement, as the main function must return an integer value. You may also see a warning about the unused variable X. This output is basically your to-do list to get the code to build, and that's only to build. There's no guarantee that fixing these program mistakes would make the program bug free. The point here is to use these tools to help find and fix the bugs in your code.