I know that sometimes if you don’t initialize an int, you will get a random number if you print the integer.
But initializing everything to zero seems kind of silly.
I ask because I’m commenting up my C project and I’m pretty straight on the indenting and it compiles fully (90/90 thank you Stackoverflow) but I want to get 10/10 on the style points.
So, the question: when is it appropriate to initialize, and when should you just declare a variable:
int a = 0;
vs.
int a;
A rule that hasn’t been mentioned yet is this: when the variable is declared inside a function it is not initialised, and when it is declared in static or global scope it’s set to 0:
However – for readability I would usually initialise everything at declaration time.
If you’re interested in learning this sort of thing in detail, I’d recommend this presentation and this book