Local Labels In C

We have the option to employ traditional labels with macro functions. If these macro functions are utilized multiple times within a single function, an error will be triggered due to the functional scope, where the labels are redundantly declared within the function.

When dealing with intricate macros, leveraging the local label functionality proves advantageous. Employing a goto statement can aid in exiting nested loops present within a macro. Nonetheless, in scenarios where the macro is iterated multiple times within a sole function, the label is redundantly defined within that function, rendering it unsuitable for serving as a conventional label with function-wide scope. This predicament is circumvented by utilizing a local label.

Pseudo code:

Example

#define fxnMacro(parameters...)
do {												 
		if (condition == true)							 
				goto label;								 
		<----some code------ >								 
														
		label:											 
			<----some code------ >							 
} while(0);											 

Void helper()
{
	<----some code------ >
	fxnMacro(parameters...);
	<----some code------ >
	fxnMacro(parameters...);
}

Explanation:

In the provided code snippet, there is a function macro that incorporates labels and code segments. Within the auxiliary function, the macro function is invoked twice, resulting in inevitable compilation errors. This occurs because the compiler becomes perplexed by the presence of a duplicate label declaration.

So we have the option to utilize local variables in place of traditional labels.

Syntax:

Example

__label__ labelName;

Local labels need to be declared at the beginning of the block, prior to any other declarations.

C Example:

Example

#include <stdio.h>
#include <string.h>

#define FUNCTION_MACRO(str)									 \
do {														 \
		__label__ isEmpty, is_not_empty, terminate;					 \
		if (strlen(str))									 \
				goto is_not_empty;								 \
		else												 \
				goto isEmpty;									 \
																\
		is_not_empty:											 \
				printf("string = %s\n", str);				 \
				goto terminate;									 \
		isEmpty:												 \
				printf("string is empty\n");				 \
		terminate: ;												 \
} while(0);													 \

int main()
{
		char str[30] = {'\0'};
		FUNCTION_MACRO(str);
		strcpy(str, "JavaLogic Practice");
		FUNCTION_MACRO(str);
		return 0;
}

Output:

Output

[Program Output]

Explanation:

In the provided code snippet, there exists a macro function designed to assess the emptiness of a string by evaluating its length. The function is designed to accept a single string input parameter. If the length of the string is determined to be zero, the output will display a message indicating that the string is empty; otherwise, a different statement will be printed.

We have employed three internal identifiers in the preceding code snippet, namely isEmpty, isnotempty, and terminate.

We have employed the goto statement under various conditions to transfer control from one label to another. Within the main function, we have initialized a character array with a size of 30 elements.

At first, when the string is void, this macro function will be employed to handle the empty string scenario, triggering the display of a message tailored for empty strings.

Next, in the subsequent line, we have allocated a different string to this particular string and invoked the macro function, which will display the relevant string.

So, the macro function was utilized twice within the identical function; however, no compilation errors were encountered due to the local scope of the labels.

We possess pseudocode for locating any item within an array:

Example

#define SEARCH(arr, targetValue)                     \
({                                                \
  __label__ res;                                \
  typeof (targetValue) _SEARCH_target = (targetValue);      \
  typeof (*(arr)) *_SEARCH_array = (arr);     \
  int i, j;                                       \
  int value;                                      \
  for (i = 0; i < max; i++)                       \
    for (j = 0; j < max; j++)                     \
      if (_SEARCH_array[i][j] == _SEARCH_target)  \
        { value = i; goto res; }                \
  value = -1;                                     \
 res:                                           \
  value;                                          \
})

Explanation:

In the provided pseudo-code, the objective is to locate a specific element within the target array. To achieve this, the macro function requires the array and the search value as input parameters.

In the function, a local label named res is utilized to hold the value if it is discovered in the array. Otherwise, -1 is assigned to the result.

Input Required

This code uses input(). Please provide values below: