I know that pre C99 compilers needed variables to be declared at the beginning of a function to calculate stack size. Then the requirement was lifted. Nowadays, besides backwards compability, is there any benefit from still sticking to that scheme, or maybe is declaring variables only when they are needed and where they are needed better? (for instance in an if statement, where one branch requires a variable but second one doesn't)
-
2This feels somewhat opinion-based. That being said, for cleanliness, I'd always suggest declaring variables only from the point they are needed. If you need a variable only in a single block, why declare it outside that block? – Thomas Jager Mar 06 '21 at 15:53
-
@ThomasJager: There are coding standards with MISRA being the most common which disallow mixing code and declarations. However, I agree this is too broad and should be closed. You should have enough rep to close-vote yourself, I think? – too honest for this site Mar 06 '21 at 20:36
-
@toohonestforthissite Could you please point to the specific MISRA rule that requires variables to be declarared only at the beginning of a function or a block scope? – Bob__ Mar 07 '21 at 09:50
-
@ThomasJager: Very opinion based. When trying to work with unfamiliar code people tend to have 2 modes - trying to understand the logic and control flow (where variable types aren't important and "intermingled declarations" add worthless clutter and makes it harder to understand) and looking for things like overflows in expressions (where "intermingled declarations" means you have to search for declarations that could be buried anywhere). The worst case is "same name used in multiple declarations with different types (at different scopes)"; which isn't allowed by "all declarations at top". – Brendan Mar 07 '21 at 10:12
-
@Bob__ As the MISRA norm (not a standard, caution if you are German!) is not for free and I only can check them if the customer requires it, no, I can't right now (I try to get projecs with resonable coding styles). But if you are bound to MISRA, you should be able to check yourself, shouldn't you? – too honest for this site Mar 07 '21 at 15:10
-
2How is this opinion based. I want facts, not opinions. And it turned out that no, there are no real speed-wise or performance-wise benefits. Don't know why you people always flag stuff as opinion based. – Franciszek Balcerak Mar 07 '21 at 15:57
-
1The only opinion-based thing in this question are the comments. – Klesun Mar 18 '21 at 19:32
2 Answers
The answer is no. There is no compilation or other computing benefit from declaring identifiers at the beginning of a function.
Good modern compilers analyze where values are used in code, so the locations of declarations are irrelevant as long as they do not affect semantics (such as moving a declaration into a compound statement, reducing its scope).
In some cases, there may be benefit from telling a reader what you are going to do at the beginning of a function or block. Generally, it is beneficial to declare identifiers just where they are needed, as this tends to reduce the number of things a reader has to think about at one time. However, if there is some pattern or rhyme and reason to the algorithm a function is going to perform, then showing some aspects of that at the beginning can help the reader comprehend it.

- 195,579
- 13
- 168
- 312
The C language has permitted declaration of variables in any statement block from at least 1978 as described in The C Programming Language Section 4.8 Block Structure. This is also described in Where you can and cannot declare new variables in C?.
More recent revisions, in addition to permitting declarations at the beginning of any block, allow declarations to be placed later in a block but does not permit forward references. The size of arrays can also be dynamically defined and are not restricted to constant expressions.
Whether there is any benefit opens the question of to whom. I can think of reasons where there is benefit:
- Simpler functions may not benefit from the additional design effort of structuring of variable declarations,
- Functions with locally scoped variables can silently hide declarations at a higher level,
- Automatic variable declarations that contain dynamic sizing requiring checking before being allocated,
- Readers of the code can see the identifiers referenced in a function in one location - often with comments that describe behaviour, and,
- Code translated from other languages without this feature looks more like the original.
Conversely, I can think of several reasons why this is not beneficial:
- A naïve compiler can use this information to improve stack frame allocation without static analysis (particularly with large variables),
- A mechanical code generator can avoid name space clashes by declaring variables within a smaller scope without checking, and,
- Long functions benefit from local scoping by avoiding potential use of values not valid throughout a function body.

- 3,529
- 27
- 45
-
This is not what the question is about. Mixing code and declarations has indeed been allowed only since C99. Until then, declarations were only allowed at the beginning of a block (sidenote: a block _is_ a statement by itself. A "statement block" does not exist in the language). FYI: the most obvious reason to relax this are VLAs which required the length to be checked before the declaration for reliability reasons. – too honest for this site Mar 06 '21 at 20:40
-
@too honest for this site The OP asked about whether declaring variables other than at the beginning of the function was useful. I indicated this had been supported since K&R C. There was no indication that this was about declaring variables inline in text, just in multiple places inside the function. I've added your additional use case to the answer. – Pekka Mar 07 '21 at 08:40
-
I did not say you add some more information, just that your statement about the standard is incomplete and missleading for a beginner. OP clearly asked about mixing code and declarations which is definitively more than just declarations at the start of a block, but what I pointed out. Wrt your edit: C does not have a concept of a stack, less a stack-frame. You won't even find that word in the standard. So refering to "stack frame variables" is pointless. This is not even a common term. You mean ´auto´ or *automatic* variables which is widely used. – too honest for this site Mar 07 '21 at 15:16
-
@too honest for this site Your corrections to the answer continue to be appreciated and improve the result. I don't see how you can't reasonably interpret "where one branch requires a variable" to mean declarations at the start of a block. – Pekka Mar 07 '21 at 21:24
-
Good try pulling that out of the context. The full sentence was "(for instance in an if statement, where one branch requires a variable) but second one doesn't)". If the parentheses are not enough as an indicator this is just one example, "for instance" should make it obvious. Still disagreeing? (Oh, and it not even states said variable needs to be declared at the start of the block - not even nitpicking about the term "branch" being non-standard and ambiguous) – too honest for this site Mar 07 '21 at 21:57