Resolving the "a label can only be part of a statement..." error

The “a label can only be part of a statement and a declaration is not a statement” error occurs in C when it encounters a declaration immediately after a label.

The C language standard only allows statements​ to follow a label. The language does not group declarations in the same category as statements.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Code

Consider the following code snippet that throws this error:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Note how str2 is declared immediately after the Here: label on line 88. The solution to this error​ is to add a semi-colon after the label. The compiler will translate it as a blank statement and ​not throw the error. The following code snippet implements this fix:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

The error can also occur when using switch statements in C, as ​the language treats cases similar to labels. Consider the following error scenario:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

The solution is the same as before; a​ semi-colon needs to be added after the case 'a' statement on line 77. Alternatively, the entire case can be enclosed in curly braces to circumvent the error. The following code snippet implements both ways of fixing this error:

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved