Search code examples
cvariable-declaration

Is there a reason why declarations and assignments of variables are on different lines in K&R?


I'm starting to read The C Programming Language, and I noticed that the declarations of variables and the assignment of them are on different lines. For example, from page 16:

int c;
c = getchar();

Is there a reason why int c = getchar(); is not written (in general, why assignment is not on the same line as declaration)? It seems to work fine when I run it instead of the former.


Solution

  • If you are reading second edition of the book, it is mentioned on page 86.

    In effect, initializations of automatic variables are just shorthand for assignment statements. Which form to prefer is largely a matter of taste. We have generally used explicit assignments, because initializers in declarations are harder to see and further away from the point of use.