I spent the last 2 days staring at a library function I had written trying to figure out why it was seg-faulting. It worked fine in another program I was hacking at so it was annoying to say the least.
UNIT TESTS!
Had I not been dumb I would have made sure the file opened successfully. I had manually created the file and put the wrong file name in my global #define for that string. As such it failed to open every single time. Since I didn't have a prompt or error break on failure to open it would just segfault when my function to count the number of lines ran. I wasted a couple days getting pissed off for no reason.
The C course I started my journey with was on Udemy. The instructor wouldn't stop talking about unit tests at one point. Probably the most important lesson I've learned thus far now. Especially after wasting so much time for no reason.
Validate everything. While there is probably a point where it gets out of hand it will only help you to include a test for pretty much every variable and bit of data you compute in your stuff.
Code:
if (!fileptr)
{
printf("unable to open file!\n");
exit(1);
}
This small segment of code in this case, which I actually had created a library function with more data output about the error some time ago would have saved me 2 days of screwing around. And likely my blood pressure a bit as well.