I'm curious about how the experts here feel about using printf()/cerr for debugging purposes. Assume a new user, would you recommend printing vs. learning the debugger at a newbie's stage of development.
And what do you think of this writeup. Is the author correct -- especially for the new student?
Or how about this one (specifically at printf debugging heading)?
They don't seem unreasonable.
Printing things is easy to explain and produces and obvious benefit without having to learn yet another tool. It's also fine for the single file assignments with only a couple of functions and a handful of variables.
Debuggers come in when programs get larger, and repeated attempts at inserting printf() in the wrong place causes people to seek out a better way.
Thanks Salem, that was my contention when someone tried to prove me "wrong" about suggesting printf(). Thought I'd get another opinion from respected programmers...
>I'm curious about how the experts here feel about using printf
>()/cerr for debugging purposes.
As with any debugging technique, debug prints aren't always appropriate. A big problem is that the actual machine code is changed between a production and debug compile. Simply calling printf could hide the bug and fudge the process into working for the debug compile but its absence would cause the production code to fail. You could argue that this is still valuable debugging information, but it's frustrating, and in such a case a debugger would be vastly more effective.
Actually, I think we had a problem like that not too long ago (maybe on another forum), where printf was the deciding factor in whether an unrelated floating-point operation worked or didn't work.
>Assume a new user, would you recommend printing vs. learning
>the debugger at a newbie's stage of development.
Yes. It's easy to understand and easy to implement without having to break from the normal process of learning the language. But I would call it a crutch and encourage learning a debugger as soon as the beginner is comfortable.
> Actually, I think we had a problem like that not too long ago (maybe on another forum)
Yeah, I remember :)
> As with any debugging technique, debug prints aren't always appropriate.
Not to mention all the "my debug build works, but my release build doesn't" posts we see from time to time.
It's considerably more interesting when it's "my release build works, but my debug build doesn't" ;)