goto and labels in C

By: Norman Chap  

C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto statement is never necessary, and in practice it is almost always easy to write code without it.

Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop. Thus:

       for ( ... )
           for ( ... ) {
               if (disaster)
                   goto error;
       /* clean up the mess */

This organization is handy if the error-handling code is non-trivial, and if errors can occur in several places.

A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function.

As another example, consider the problem of determining whether two arrays a and b have an element in common. One possibility is

       for (i = 0; i < n; i++)
           for (j = 0; j < m; j++)
               if (a[i] == b[j])
                   goto found;
       /* didn't find any common element */
       /* got one: a[i] == b[j] */

Code involving a goto can always be written without one, though perhaps at the price of some repeated tests or an extra variable. For example, the array search becomes

   found = 0;
   for (i = 0; i < n && !found; i++)
       for (j = 0; j < m && !found; j++)
           if (a[i] == b[j])
               found = 1;
   if (found)
       /* got one: a[i-1] == b[j-1] */
       /* didn't find any common element */

With a few exceptions like those cited here, code that relies on goto statements is generally harder to understand and to maintain than code without gotos. Although we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all.

This tutorial is an excerpt from K&R - The C programming Language

Archived Comments

1. nclxhaCep
View Tutorial          By: ntlyidCep at 2017-09-13 05:51:21

2. Potrafisz t_ dole kiedy typ wyst_pi_ do
Twojego przedsi_biorstwa aukcji za_ przemwi_ nam, i_

View Tutorial          By: strona internetowa at 2017-07-29 20:46:53

3. ClaytonMaymn
View Tutorial          By: ClaytonMaymn at 2017-07-23 22:04:58

4. Norman, it would be good to credit where your write up above comes from. Its as originally published
View Tutorial          By: K&R at 2014-03-20 11:41:56

5. The C programming language by Brian Kernighan and Dennis Ritchie. No credits have been given. Clear
View Tutorial          By: harsh at 2012-09-11 15:07:50

6. Except for when you're programming low level hardware and you're worried about EMC. EM can cause cor
View Tutorial          By: Jonathan H at 2012-05-10 08:46:48

7. This is an uncredited, nearly exact copy of Brian W. Kernighan and Dennis M. Ritchie's "The C P
View Tutorial          By: HC Saustrup at 2012-03-26 11:24:40

Most Viewed Articles (in C )

Latest Articles (in C)

Comment on this tutorial