×

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

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.

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Typedef vs define in C

Typedef VS define in C Typedef In the C programming language, a keyword called typedef can be used to give a type a new name. In other words, it is used to...

5 minutes read.

gets() function in C

gets() is a pre-define or built-in function present in the stdio.h header file. stdio stands for standard input and output. gets() function is used to read a stream and store...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Classification of Programming language

 Classification of Programming language  The programming language represents a set of instructions compiled along with each other to perform a specific task provided by the CPU (Central Processing Unit). A programming...

3 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Fibonacci Series in C Using For Loop

In this C article, we will let you know about the procedure of displaying the Fibonacci series of the first “n” positive integers. The syntax of for loop used in the...

4 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

Fibonacci series in C

We have learned about the Fibonacci series in mathematics. For a quick recap, A Fibonacci series is the sequence of numbers following a certain pattern i.e., the next number should...

3 minutes read.

Quick sort in C

Quick-sort, in the same way, like a merge sort, follows the principle of the divide and conquer algorithm. It then picks an element as pivot and partitions, and the given...

4 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Array Of Structures in C

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

4 minutes read.