What purpose does it serve?
Just read an example in a book where the author has done so.
int numOfGuesses=0;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The automatic assignment to zero only applies to members, not to local variables. If it is a local variable and the
= 0is omitted then that variable has no value, not even zero. Attempting to use the value before it is assigned will result in a compile error. For example this code attempts to use an uninitialized local variable:and produces this compile error:
Whereas this code using a member works and outputs zero:
For members I tend to assign to zero explicilty if my code uses the fact that the initial zalue is zero, and omit the assignment if my code doesn’t use the initial value (for example if it the value is assigned in the constructor or elsewhere). The result is the same either way, so it’s just a style issue.