In C++ you are not allowed to bypasses declarations with initialization but it seems to be allowed in C99. You can have gcc
warn you about if you either use -Wjump-misses-init
or -Wc++-compat
. This is covered in the gcc
docs section Options to Request or Suppress Warnings and says:
Warn if a goto statement or a switch statement jumps forward across the initialization of a variable, or jumps backward to a label after the variable has been initialized. This only warns about variables that are initialized when they are declared. This warning is only supported for C and Objective-C; in C++ this sort of branch is an error in any case.
-Wjump-misses-init is included in -Wc++-compat. It can be disabled with the -Wno-jump-misses-init option.
Note, this also applies to declarations inside a switch statement. One way to work around this is to create a new scope using {}
.
In the Annex I the draft C99 standard suggests this as warning, it says:
An implementation may generate warnings in many situations, none of which are specified as part of this International Standard. The following are a few of the more common situations.
and includes the following bullet:
A block with initialization of an object that has automatic storage duration is jumped into (6.2.4).