Which is the better way of declaring variables?
1.
int i,j,k;
2.
int i;
int j;
int k;
Can anybody explain which is the better way and why?
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.
This is ultimately a matter of personal taste. It doesn’t matter to the compiler or your program either way.
If you’re working on a team with other programmers, the important thing is that you follow their established standards. If you’re maintaining a base of existing code, follow the style already established in the source. Otherwise, you’re free to make your own decisions about how to format your code.
Personally, I prefer the second style. It makes it much clearer to me what the types are of each variable. Additionally, if you’re working in C or C++ and declaring pointers, it’s important to keep in mind that
will only declare
ias a pointer to anint(see this question for more discussion). Using the second declaration style makes it completely unambiguous, which is always better for long-term maintainability. The amount you’re saving by squashing all variable declarations to one line doesn’t seem worth it to me.