For these types of situations you can really only learn by doing. You can litter code with printf() [although this can actually hide bugs too if you're very unlucky], comment out code until you come to a minimal program that exhibits the buggy behaviour and then just persevere.