4 ms·
What you say is true, but this case could easily trigger a warning about possible use before assignment. The compiler doesn't have (and can't) to prove that ei
by bmm6o 11y ago
What you say is true, but this case could easily trigger a warning about possible use before assignment. The compiler doesn't have (and can't) to prove that either branch will always be taken at runtime. And there's no benefit to code like this anyway, just write it so it's obviously to be assigned. Your coworkers will thank you.
- MereInterest 11y agoI completely agree. I was, instead, trying to give an example of why a compiler can't just "detect undefined behavior" as the grandparent suggested.
- bmm6o 11y agoAnd really, the main benefit to not assigning a value in the declaration is to get the compiler to tell you if there is a code path that misses an assignment. int i; if (foo || bar) i = 1; else if (baz) { if (!quux) goto fail; i = 2; } printf("%d", i); // not all code paths assign a value