Everybody who has programmed in C programming language must be aware about “goto” and “labels” used in C to jump within a C function. GCC provides an extension to C called “local labels”.
Conventional Labels vs Local Labels
Conventional labels in C have function scope. Where as local label can be scoped to a inner nested block. Therefore, conventional Labels cannot be declared more than once in a function and that is where local labels are used.
A label can appear more than once in function when the label is inside a macro and macro is expanded multiple times in a function. For example if a macro funcMacro() has definition which involves jump instructions (goto statement) within the block and funcMacro is used by a function foo() several times.
In such a function foo() , the function macro will be expanded two times.
This will result in having more than one definition of label ‘x’ in a function, which leads to confusion to the compiler and results in compilation error. In such cases local labels are useful.
The above problem can be avoided by using local labels. A local label are declared as below:
Local label declarations must come at the beginning of the block, before any ordinary declarations or statements.
Below is C example where a macro IS_STR_EMPTY() is expanded multiple times. Since local labels have block scope and every expansion of macro causes a new do while block, the program compiles and runs fine.
string is empty string = geeksForgeeks
This article is contributed by Kashish Bhatia. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Can we access global variable if there is a local variable with same name?
- Data type of case labels of switch statement in C++?
- Local Classes in C++
- Introduction to the C99 Programming Language : Part I
- Jagged Array or Array of Arrays in C with Examples
- Introduction to the C99 Programming Language : Part III
- Introduction to the C99 Programming Language : Part II
- Features of C Programming Language
- Difference between Increment and Decrement Operators
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- Predefined Macros in C with Examples
- How to create GUI in C programming using GTK Toolkit
- ctype.h(<cctype>) library in C/C++ with Examples