调试
Learning Goals
- Become familiar with C syntax
- Learn what C compiler error messages 意思
- Get practice debugging

背景
There are two kinds of errors that can occur when writing a program. The first errors you are likely to encounter are syntactical errors. In addition to syntactical errors, there can also be logical errors, which we’ll take a look at soon.
In 计算机科学, syntax is important for a computer to understand what you are telling it to do. Each 编程 language has its own syntactical rules, which include the combination of both words and punctuation.
This 实验 starts with 分发代码 which has several syntactical errors. The idea is for you to try to compile (make) the program, learn to interpret the rather cryptic error messages output by the compiler, and 调试 the program.
- 提示
- You’ll probably see the first error after trying to compile
debug.cwill bedebug.c:9:5: error: use of undeclared identifier 'name'. The9afterdebug.c:意思s there is a problem on line 9. Why do you think is saysundeclared identifier? - You可能想要 to look for errors such as missing symbols, missing libraries, missing variable declarations.
- If you are still stuck, try typing into the terminal
help50 make debug.
- You’ll probably see the first error after trying to compile
演示

入门
- Log into cs50.dev using your GitHub account.
- Click inside the terminal window and execute
cd. - At the
$prompt, typemkdir debug - Now execute
cd debug - Then copy and paste
wget https://cdn.cs50.net/2022/fall/实验s/1/debug.cinto your terminal to 下载 this 实验’s 分发代码. - Now try compiling this program, by typing
make debugand see what happens! - You most likely see an error that says:
debug.c:9:5: error: use of undeclared identifier 'name' - This 意思s there is an error that has something to do with an “undeclared identifier”, on line 9. Fix this bug, and then try to compile again. 请记住 that debugging is an iterative process. You可能需要 to fix an error, compile, then fix another error, compile again, multiple times!
Thought 问题
- Why do you think C (as well as other 编程 languages) have such specific rules regarding syntax?
如何测试 Your Code
你的程序应该 behave per the例子below.
debug/ $ ./debug
What is your name? Carter
Where do you live? Cambridge
Hello, Carter, from Cambridge!
debug/ $ ./debug
What is your name? Margaret
Where do you live? New York
Hello, Margaret, from New York!
You can check your code using check50, a program that CS50 will use to test your code when you 提交, by typing in the following at the $ prompt. But be sure to test it自己 as well!
check50 cs50/实验s/2023/x/debug
Green smilies 意思 your program has passed a test! Red frownies will indicate your program output something unexpected. Visit the URL that check50 outputs to see the input check50 handed to your program, what output it expected, and what output your program actually gave.
To evaluate that the style of your code (indentations and spacing) is correct, type in the following at the $ prompt.
style50 debug.c
如何提交
No need to 提交! This is an 可选 练习题.