how to use a label??

Discussion in 'C Programming' started by hisam, Apr 18, 2006.

  1. hisam

    hisam Guest

    How to use a label in c?? also how to call???
     
    hisam, Apr 18, 2006
    #1
    1. Advertising

  2. hisam wrote:
    > How to use a label in c?? also how to call???
    >

    Any C book will tell you how to do this. Maybe try Google if you do not
    have a paper book.
     
    void * clvrmnky(), Apr 18, 2006
    #2
    1. Advertising

  3. hisam

    Michael Mair Guest

    hisam schrieb:
    > How to use a label in c?? also how to call???


    From the C99 standard:

    ,-- 6.8.1 Labeled statements --
    Syntax
    1
    labeled-statement:
    identifier : statement
    case constant-expression : statement
    default : statement

    Constraints
    2 A case or default label shall appear only in a switch statement.
    Further constraints on such labels are discussed under the switch
    statement.
    3 Label names shall be unique within a function.
    Semantics
    4 Any statement may be preceded by a prefix that declares an
    identifier as a label name.
    Labels in themselves do not alter the flow of control, which
    continues unimpeded across them.
    `----

    Usage with goto: Labels have to precede a statement (not a
    declaration) and are visible within a function, regardless of
    nesting of block scopes.

    Example [untested]:

    int main (void)
    {
    int i;

    loopinit:
    i = 0;
    loopcontinuation:
    if (i < 10)
    goto loopend;
    {
    /* Do something */
    if (i==5)
    goto loopupdate;
    /* Do something else */
    }
    loopupdate:
    i++;
    goto loopcontinuation;
    loopend:
    ;

    return 0;
    }

    Obviously,
    int main (void)
    {
    int i;

    for (i = 0; i < 10; i++)
    {
    /* Do something */
    if (i==5)
    continue;
    /* Do something else */
    }

    return 0;
    }
    is easier on the eye.

    Case/default labels are visible only for the switch statement
    they belong to. Untested example:

    int main (void)
    {
    int i = 6, j = 7, k = 0;

    switch (i) {
    case 0:
    j *= -1;
    break;
    case 6:
    switch (j) {
    case 0:
    k = 7;
    break;
    case 7:
    k = i*j;
    break;
    }
    break;
    default:
    j = 23;
    }
    switch (k) {
    case 7:
    /* Whatever */
    break;
    case 42:
    /* Yep */
    break;
    default:
    /* Boring */
    break;
    }

    return 0;
    }


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
     
    Michael Mair, Apr 18, 2006
    #3
  4. hisam

    Neil Guest

    hisam wrote:
    > How to use a label in c?? also how to call???
    >

    C has labels

    ALabel:

    C has goto

    goto ALabel;

    C does not have call it has functions. BASIC has call.

    To invoke a function just use its name

    AFunction();
     
    Neil, Apr 19, 2006
    #4
  5. hisam

    Al Balmer Guest

    On 18 Apr 2006 12:30:29 -0700, "hisam" <> wrote:

    >How to use a label in c?? also how to call???


    Others have told you how to use a label. I'll tell you when to use
    one: after at least a couple more years of C programming, when there's
    no good alternative. In the meantime, read
    http://www.acm.org/classics/oct95/

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Apr 19, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shapper
    Replies:
    2
    Views:
    772
    Scott Mitchell [MVP]
    May 7, 2005
  2. Josef K.

    <label><div></div></label> allowed?

    Josef K., May 20, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    13,755
    Josef K.
    May 22, 2005
  3. Praveen
    Replies:
    1
    Views:
    1,060
    Rob Williscroft
    Nov 5, 2006
  4. CSharpner
    Replies:
    0
    Views:
    1,143
    CSharpner
    Apr 9, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    337
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page