Javatpoint Logo
Javatpoint Logo

Local Labels in C

In programming, labels are those keywords which help to jump the program control from one function to another function. In C language, we use the goto function to implement this functionality. In C, GCC provides the concept of labels or local labels which implements the functionality of jumping from one function to another in the same program.

We can use conventional labels instead of local labels in C. Still, there are some limitations of convention labels, like the scope of any conventional label is in a function. In contrast, the scope of local labels can be expanded to any nested block. That's why conventional labels cannot be declared more than once in a function, whereas we can use local labels more than once.

We can use conventional labels using macro functions, and if we use these macro functions more than once in the same function, then it will throw an error because the scope is functional, and we are declaring the labels more than once in the same function.

For complex macros, the local label feature is beneficial. A goto can be helpful for quitting nested loops if a macro contains them. However, if the macro can be repeated several times within a single function, the label is multiplicatively defined in that function and cannot be used as a regular label whose scope is the entire function. This issue is avoided with a local label.

Pseudo code:


In the above code, we have a function macro where we use labels and some code. In the helper function, we called the macro function twice, so there will definitely be compilation errors because the compiler is confused due to the declaration of one label more than once.

So we can use the local variables instead of conventional labels.


Declaration of the local labels should be at the start of the block before any declarations.

C Example:


Local Labels in C


In the above code, we have a macro function which will check whether a string is empty or not based on its length. It will take one string as an input parameter, and if the length of the string is zero, then we will print starting as empty otherwise, we will print another statement.

We have used three local labels in the above code, which are isEmpty, is_not_empty, and terminate.

We have used the goto function on different conditions to jump from one label to another, and in the main function, we have created an array of characters of length 30.

Initially, the string is empty, so that we will use this macro function for the empty string, and it will print the statement for the empty string.

Then in the following line, we have assigned another string to this string and called the macro function, and it will print the corresponding string.

So, we have used the macro function twice in the same function, but since the labels were local, we did not get any compilation error.

We have pseudo code to search any element in an array:


In the above pseudo-code, we will search any element which is present in the target array. So we will pass array and search value as input parameters in the macro function.

In the function, we used local label res to store the value of if we find it in the array. Otherwise, we will put -1 in the result.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA