You tagged this as C, but in another comment mention C++ and Java. I'll try to cover all of them.
From the latest online C standard:
6.2.4 Storage durations of objects
5 An object whose identiﬁer is declared with no linkage and without the storage-class
static has automatic storage duration, as do some compound literals. The
result of attempting to indirectly access an object with automatic storage duration from a
thread other than the one with which the object is associated is implementation-deﬁned.
6 For such an object that does not have a variable length array type, its lifetime extends
from entry into the block with which it is associated until execution of that block ends in
any way. (Entering an enclosed block or calling a function suspends, but does not end,
execution of the current block.) If the block is entered recursively, a new instance of the
object is created each time. The initial value of the object is indeterminate. If an
initialization is speciﬁed for the object, it is performed each time the declaration or
compound literal is reached in the execution of the block; otherwise, the value becomes
indeterminate each time the declaration is reached.
b are declared
static, nor are they declared with any explicit linkage, so they have
auto storage duration; they only exist for the lifetime of their enclosing scope (logically speaking). In practice, the storage for both
b will (usually) come from the runtime stack, which will (usually) be set aside at function entry and (usually) released at function exit. However, that's an implementation detail; as long as the behavior conforms to the standard, the exact nature of the storage (stack, heap, or otherwise) doesn't really matter.
As far as storage is concerned, it doesn't matter when
b is declared within the function; after the source code has been translated to machine code, storage will be set aside at function entry.
From the online C++ standard:
3.7.2 Automatic storage duration [basic.stc.auto]
1 Local objects explicitly declared
register or not explicitly declared
extern have automatic storage
duration. The storage for these objects lasts until the block in which they are created exits.
2 [Note: these objects are initialized and destroyed as described in 6.7. — end note ]
3 If a named automatic object has initialization or a destructor with side effects, it shall not be destroyed before the end
of its block, nor shall it be eliminated as an optimization even if it appears to be unused, except that a class object or its
copy may be eliminated as speciﬁed in 12.8.
For primitive types, the behavior is almost identical to C; storage is (usually) allocated from the runtime stack at function entry, and (usually) released at function exit. For instances of class types, it gets a little more complicated.
From Section 2.6 of the Java Virtual Machine Specification:
A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception). Frames are allocated from the Java Virtual Machine stack (§2.5.2) of the thread creating the frame. Each frame has its own array of local variables (§2.6.1), its own operand stack (§2.6.2), and a reference to the run-time constant pool (§2.5.5) of the class of the current method.
2.6.1. Local Variables
Each frame (§2.6) contains an array of variables known as its local variables. The length of the local variable array of a frame is determined at compile-time and supplied in the binary representation of a class or interface along with the code for the method associated with the frame (§4.7.3).
In all three cases, the behavior will be the same (allowing for differences in how
main is defined; the posted snippet isn't 100% correct for any of C, C++, or Java).