Label (computer science)

From Wikipedia, the free encyclopedia
  (Redirected from Label (programming language))
Jump to: navigation, search

A label in a programming language is a sequence of characters that identifies a location within source code. In most languages labels take the form of an identifier, often followed by a punctuation character (e.g., a colon). In many high level programming languages the purpose of a label is to act as the destination of a GOTO statement.[1][2] In assembly language labels can be used anywhere an address can (for example, as the operand of a JMP or MOV instruction).[3] Also in Pascal and its derived variations. Some languages, such as Fortran and BASIC, support numeric labels.[4] Labels are also used to identify an entry point into a compiled sequence of statements (e.g., during debugging).

C[edit]

In C a label identifies a statement in the code. A single statement can have multiple labels. Labels just indicate locations in the code and reaching a label has no effect on the actual execution.

Function labels[edit]

See also: goto

Function labels consist of an identifier, followed by a colon. Each such label points to a statement in a function and its identifier must be unique within that function. Other functions may use the same name for a label. Label identifiers occupy their own namespace - one can have variables and functions with the same name as a label.

void foo(int number)
{
    if (number < 0)
        goto error;
    bar(number);
    return;
error:
    fprintf(stderr, "Invalid number!\n");
}

Here error is the label. The statement goto can be used to jump to a labeled statement in the code. After a goto, program execution continues with the statement after the label.

Switch labels[edit]

Two types of labels can be put in a switch statement. A case label consists of the keyword case, followed by an expression that evaluates to integer constant. A default label consists of the keyword default. Case labels are used to associate an integer value with a statement in the code. When a switch statement is reached, program execution continues with the statement after the case label with value that matches the value in the parentheses of the switch. If there is no such case label, but there is a default label, program execution continues with the statement after the default label. If there is no default label, program execution continues after the switch.

switch (die)
{
default:
    printf("invalid\n");
    break;

case 1:
case 3:
case 5:
    printf("odd\n");
    break;

case 2:
case 4:
case 6:
    printf("even\n");
    break;
}

Within a single switch statement, the integer constant associated with each case label must be unique. There may or may not be a default statement. There is no restriction on the order of the labels within a switch. The requirement that case labels values evaluate to integer constants gives the compiler more room for optimizations.

See also[edit]

References[edit]

  1. ^ C Standard section 6.8.6.1 The goto statement
  2. ^ "GOTO Statement QuickSCREEN". Microsoft. 1988. Retrieved 2008-07-03. 
  3. ^ O. Lawlor. "nasm x86 Assembly". Retrieved 2008-07-03. 
  4. ^ "Differences Between GW-BASIC and QBasic". 2003-05-12. Retrieved 2008-06-28.