If you are writing C++ or C starting from the C99 standard onwards (which isn’t supported by some non-standard-compliant compilers such as Microsoft’s Visual C++), then you should avoid declaring all the variables at the top of the program or the subroutine (a practice that is referred to as “pre-declaration”) and instead declare them when they are needed and with an immediate initialisation/definition using the =
operator.
E.g:
# Bad code int main(int argc, char * argv[]) { const char * name; int i; name = "Rupert"; for (i=1 ; i<=10 ; i++) { printf ("Hello %s - No. %d!\n", name, i); } return 0; }
Should be replaced with:
int main(int argc, char * argv[]) { const char * const name = "Rupert"; for (int i=1 ; i<=10 ; i++) { printf ("Hello %s - No. %d!\n", name, i); } return 0; }