KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q64807: goto Label at End of Block Without ";" Invalid Syntax

Article: Q64807
Product(s): See article
Version(s): 4.x 5.x 6.00 | 5.10 6.00
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | s_quickc 1.x 2.00 2.50 s_quickasm 2.01 2.51 | mspl13_c
Last Modified: 15-AUG-1990

If a goto label appears last in a block of code, the following
compiler error will be generated:

   error C2143:  missing ';' before '}'.

The sample code below illustrates the problem.

The semicolon is required by the ANSI definition of the C programming
language. In fact, this situation is described in Section 3.6.3 of the
standard.

To eliminate the error, use proper syntax as described below.

Sample Code
-----------

#include <stdio.h>
#include <conio.h>

void main(void)
  {
     char ch;

     while(!kbhit())
        {
          printf("Want to end this loop?  (y/n)  \n");
          if((ch = getche()) == 'y')
             {
                printf("out of loop\n");
                goto mybug;
             }
          printf("More loop processing here....\n");
          mybug:   /* This line generates the syntax error      */
        }          /*       replace with: mybug:  ;             */
  }

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.