×

Local Labels in C

Anyone who has written programs in the C programming language is required to be familiar with the "go to" and "labels" used in C to navigate between functions. "Local labels" is a C extension offered by GCC.

Conventional Labels vs Local Labels

In C, standard labels have a function scope. A local label, however, can be limited to an inner nested block. Since standard Labels can only be declared once in a function, local labels are used instead.

When a label is contained within a macro and the macro is expanded numerous times in a function, the label may appear more than once in the function. For instance, suppose that the definition of the macro fun Macro() includes leap statements (go to statements) inside the block and that the function foo () repeatedly calls fun Macro. Using local labels can help you avoid the issue.

Following is a local label's declaration:

_label_label;

Local label declarations must appear before any regular declarations or statements at the start of the block.

The macro IS STR EMPTY() is enlarged numerous times in the C sample that follows. Since local labels have block scope and every expansion of a macro creates a new do while block, the programme compiles and runs without error.

#include <stdio.h>
#include <string.h>
//macro-function utilising local labels
#define IS_STR_EMPTY(str)                                       
do {                                                            
        __label__  empty, not_empty, exit;                      
        if (strlen(str))                                        
                gotonot_empty;                                 
        else                                                    
                goto empty;                                     
                                                             
      not_empty:                                              
                printf("string  = %s\n", str);           
     gotoexit;                                      
        empty:                                                  
                printf("string is empty\n");                    
        exit: ;                                                 
} while(0);                                                     
intmain()
{
        charstring[20] = {'\0'};
        //Send a blank string to the macro function
        IS_STR_EMPTY(string)
        //Non-empty string sent to the macro function
        strcpy(string, "my name is khan");
        IS_STR_EMPTY(string);
        return 0;
}          

Output:

Local Labels in C

Local Labels Declaration

The label declaration only specifies the label name; the label itself is not defined. This must be done using label: within the statements of the statement expression, as per standard procedure.

The label itself is not defined; only the label name is specified in the label declaration. This must be done using label: within the assertions of the statement expression, as is customary.

Labels as values

With the unary operator "&&," you can obtain the address of a label declared in the current function (or a contained function). The field's type is *void. As a constant, this value is usable anywhere that sort of constant is allowed. For instance:

    Void *ptr;
    ...
    Ptr=&&foo;

You must be able to jump to one to use these values. The calculated goto statement(1), goto *exp; is used to accomplish this. For instance,

goto *ptr;

Any void * expression is acceptable.

Initiating a static array to act as a jump table using these constants is one use for them:

Static void *array [] = { &&foo, &&bar, &&hack);

Then you can choose a label that has indexing, as in:

Goto *array [i];

The function of such an array of label values is very similar to that of the switch statement. If a switch statement is appropriate for the circumstance, use it instead of an array because it is cleaner.

Label values can also be used in a threaded code interpreter. The threaded code can save the labels within the interpreter function for blazing-fast dispatching.

This method enables you to go to distinct function-specific code. That will cause very unexpected things to occur. The best approach to prevent this is to never send the label address as an input and only save it in automatic variables.

Nested functions

A function defined inside another function is referred to as nested function. (GNU C++ does not support nested functions.) The name of the nested function is unique to the block in which it is defined. All the variables of the contained function that are visible at the time of its definition are accessible to the nested function. The term for this is lexical scoping.

Nested function declarations are allowed within functions in any block, prior to the block's initial statement, just as variable definitions are

By storing its location or providing it to another function, it is possible to call the nested function from beyond the scope of its name:

If the label was explicitly defined in the contained function, a nested function may jump to one that was inherited from it (see section Locally Declared Labels). Such a leap exits the nested function that performed the goto as well as any intermediary functions and immediately returns to the contained function.There is always internal linking in nested functions. It is incorrect to declare one using extern. Use auto if you need to declare a nested function before it is defined.


Related Topics

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Matrix Multiplication in C

Matrix Multiplication in C Matrix multiplication in C: Two matrices can be added, subtracted, multiplied, and divided. To do so, we take input from the consumer for row number, column number, first element matrix,...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

strcat() Function in C

Strings in c: A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The...

3 minutes read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Deadlock Detection Program in C

What is Deadlock? A deadlock is a circumstance where a group in the process is halted because they are each holding onto resources while waiting for other methods to obtain them. Think...

5 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.