## Using C Constructs
- Allocate local variables where it makes sense: at the top of the block, or at
- the point where they can be initialized. `r` is typically used for a local
- state variable, but should almost always be declared at the top of the
- function.
+ the point where they can be initialized. Avoid huge variable declaration
+ lists at the top of the function.
+
+ As an exception, `r` is typically used for a local state variable, but should
+ almost always be declared as the last variable at the top of the function.
```c
{
- uint64_t a, b;
+ uint64_t a;
int r;
- a = frobnicate();
- b = a + 5;
+ r = frobnicate(&a);
+ if (r < 0)
+ …
+
+ uint64_t b = a + 1, c;
- r = do_something();
+ r = foobarify(a, b, &c);
if (r < 0)
…
+
+ const char *pretty = prettify(a, b, c);
+ …
}
```
-- Do not mix function invocations with variable definitions in one line.
+- Do not mix multiple variable definitions with function invocations or
+ complicated expressions:
```c
{
- To determine the length of a constant string `"foo"`, don't bother with
`sizeof("foo")-1`, please use `strlen()` instead (both gcc and clang optimize
the call away for fixed strings). The only exception is when declaring an
- array. In that case use STRLEN, which evaluates to a static constant and
+ array. In that case use `STRLEN()`, which evaluates to a static constant and
doesn't force the compiler to create a VLA.
- Please use C's downgrade-to-bool feature only for expressions that are