Table of Contents
A label gives a statement a name that can be used as a destination for a goto statement. A label is followed by a colon and can appear before any statement within a function.
#include <stdio.h>
int main()
{
int number = 1;
start:
printf("%d\n", number);
number++;
if (number <= 3)
{
goto start;
}
return 0;
}
Example Output #
1
2
3
Explanation #
The label start identifies the statement immediately following it:
start:
printf("%d\n", number);
The goto statement transfers execution back to this label:
goto start;
Each time control reaches start, the current value of number is printed and then incremented.
The if condition determines whether execution jumps back to the label. When number becomes 4, the condition is false and execution continues to return 0.
A label has function scope and can be used only as a destination for a goto within the same function.