Core dumps happen for primarily two reasons. However, our C compiler adds a third reason that C usually doesn't support.
Just because we haven't done pointers yet, doesn't mean they aren't in your program. The "&variable" that MUST be in scanf is a 'pointer'. The "&" must be attached to any variable put in the call to scanf.
This is the unusual one that our compiler supports:
This is where I say, "Welcome to C"
I say it now, instead of in the very beginning, because here's where you really learn to program -- and where you really learn to debug.
Throw as many printf statements into your program as you need to track down the problem. Remember: You can remove them later before you submit your final program.
Start Big. At every major point, put a printf statment that tells you something about the state of your program at that point.
This includes, but is not limited to:
e.g., Row, Col, any Control Variables, and the Table Value you're trying to reach/print
e.g., "going into for loop", "top of while loop" "Bottom of loop" "Out of Loop", "This choice means that A equaled B", etc, etc, etc...
You can use as many statements to get as much information as you want about the program at that point of the execution.
You can also use the letter-tracing method.
You can simply put a sequence of printf statements throughout your program that represent some kind of ordering. They should be statements you can differentiate and locate easily.
For Example:
Keep on listing printf's and changing the letter so you can trace your program's execution.
With these statements all throughout your program, you can run it and get the list of letters that represents what happens first, second, third...etc. and then find the corresponding letters in your code. Looking at both, you can figure out what happened and discover the why.
No. It's not easier for me to look at it and fix it. This is part of what it's all about. You have to be able to figure out why your program works or doesn't work.
Debugging and Printf are used to MAKE SURE that your program is doing what you think it SHOULD BE doing according to the Code you *ACTUALLY* wrote.
You believe your code does the right thing. Printf statements all over your code should Prove it. And if it doesn't, it WILL tell you just where you went wrong.
This is a tool to help you find bugs in your program. We'll be learning how to do this in class.
graphics images Copyright 1998-2003 Elizabeth Fraley HTML code copyright 1998-2003 Elizabeth Fraley. Permission is given to provide these pages in their original, unaltered form online for educational purposes. They may not be republished or included on a CDROM, disk or other media or with any published work without the express permission of the copyright holder (this includes FAQ books).