1

I know there must be a duplicate, but I didn't find anything. In C, I see a lot of code examples where the authors only assign values after the declaration of the variable, is there a good reason for doing that?

int main(void)
{
    int x; // declare "x"
    x = 5; // assign 5 to "x"

    return 66;
}

And what's the different between that and just declaring and assigning a value in one line?

int main(void)
{
    int x = 5; // declare "x" and assign in the same line

    return 66;
}
Dudu Faruk
  • 43
  • 1
  • 5
  • 1
    The final effect is the same. And `int x;` is not a declaration, it's a definition. – Arkadiusz Drabczyk Sep 27 '20 at 19:14
  • @ArkadiuszDrabczyk So what's *declaration*? – Dudu Faruk Sep 27 '20 at 19:15
  • 1
    The second variant (with the initialization at definition site) is less risky. Even if the definition and assignment are right next to each other now (as shown in the first example), future programmers on the code might add other statements in between, and then the risk of using the variable uninitialized increases. So I recommend the second variant. But it's still a matter or personal preference and subjective. – Some programmer dude Sep 27 '20 at 19:16
  • For local variables, something like `int x;` is both a declaration (it introduces the name `x` as being a variable of the type `int` to the compiler) and a definition (it tells the compiler that the variable `x` needs space allocated for it in the running program). For global variables one have to deal with separate declarations, definitions and [tentative definitions](https://stackoverflow.com/questions/3095861/about-tentative-definition). – Some programmer dude Sep 27 '20 at 19:21
  • 3
    They are the same. The first way is more old style: in C89 you are had to declare all variables at the beginning of the function, so it was common to declare without assigning a value. – tuket Sep 27 '20 at 19:21
  • @Someprogrammerdude -- I can't argue that what you say is formally correct, at least for C. However, almost everybody I know who writes software refers to "int x" as a "declaration". It's used that way in a formal sense in the Java Language Specification, for example, which does not use the term "definition" at all. While it would, no doubt, facilitate communication if we all used the same vocabulary, I've found it impractical to insist on it. – Kevin Boone Sep 28 '20 at 07:53

1 Answers1

0

It's mostly a matter of style these days. In many cases it's less error prone to assign a value to a variable at the point where it's first used, and perhaps as close to its point of first use as possible.

On the other hand, I've certainly seen cases where an algorithm is easier to follow when it isn't dotted with statements that introduce new variables. Collecting up all these statements before the main body of the algorithm may allow the algorithm itself to fit on a single page/screen.

And it seems pointless to assign an initial value to a variable in a construction like this:

int foo;
if (bar == 42)
  foo = 1;
else
  foo = -1;
Kevin Boone
  • 4,092
  • 1
  • 11
  • 15