1

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
laffytaffy
  • 125
  • 5
  • 4
    It's just the style of the author, there's no technical difference. – Barmar Aug 04 '21 at 03:19
  • That's an older style. Variables used to be declared at the start of the scope block in which they were used. Now variables are declared when and where they are needed. – LEF Aug 04 '21 at 03:22
  • 2
    Actually, this is the way that C used to be written. Back in the days before standardization, some C compilers required you to declare variables on a different line than the definition. – Guy Marino Aug 04 '21 at 03:22
  • Actually, the original K&R C spec had the variable declarations before the curly brackets. See [this post](https://stackoverflow.com/questions/3092006/function-declaration-kr-vs-ansi) for more info. – Guy Marino Aug 04 '21 at 03:23
  • 3
    @GuyMarino That's only for the function parameters, not local variables. – Barmar Aug 04 '21 at 03:24
  • Be aware that if you put a `static` before your variable definitions, these 2 versions will have different meaning. – Gerhardh Aug 04 '21 at 07:23
  • Aside from the history, note that initialization in a declaration is **not** assignment. It shares some appearance with an assignment (like the `=`), and it shares some semantics with an assignment (like conversion to the target type), but it has some differences. `static int x = 3;` will store `3` in `x` only once, when `x` is created at the beginning of program execution, not each time the definition is reached during execution. And `int *p = &x;` puts `&x` in `p`, not in `*p`. – Eric Postpischil Aug 04 '21 at 12:45

3 Answers3

5

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.

kjh6b6a68
  • 499
  • 2
  • 8
  • Note that the last argument no longer holds - since C99 declarations can occur at first point of use (or rather at the minimal necessary scope of use). It was never a good idea to leave a variable unitialised in any case - even if you never use the value before reassigning it. It explains the authors' thinking, but does not excuse it - even if they did invent the language! – Clifford Aug 05 '21 at 19:39
3

It was once the case in C that you could only declare variables at the start of a block (immediately after the {). This was because the compiler technology was just not up to the task of arbitrarily positioned variable declarations yet.

I'm pretty sure that originally you couldn't initialize a variable with a function call early on, but by the time the writing of K&R that was already gone. But old habits stick around long after they're needed, so that's what it was.

In C '99 and newer, variable declarations can go anywhere statements can, and also in the initialization step of a for loop.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • C 1990 required declarations to be only at the start of a compound statement (C 1990 6.6.2). Initializers did not have to be constant expressions except for static objects or aggregates or unions (6.5.7). C 1999 allowed them to be anywhere (C 1999 6.8.2). Initializations are older; they were in K&R first edition, so that’s 1978. – Eric Postpischil Aug 04 '21 at 12:51
  • @EricPostpischil: I briefly had access to older C than K&R C and that's what I'm talking about "early on". – Joshua Aug 04 '21 at 13:55
  • My comment does not say that initializations were not in C or that you did not have access. It provides additional information you could edit into the answer. – Eric Postpischil Aug 04 '21 at 14:00
  • @EricPostpischil: I see. – Joshua Aug 04 '21 at 14:07
1

The original version of the C programming language did not support the newer syntax. Later editions of the book added some of the new syntaxes, but there have been new versions of C since the last edition of the book, so not all of them are there even in the last edition.

user10489
  • 410
  • 1
  • 3
  • 12