goto and labels in C

By: Norman Chap Emailed: 1589 times Printed: 2039 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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;
           }
       ...
   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 */
       ...
   found:
       /* 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] */
       ...
   else
       /* 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


C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

Except for when you're programming low level hardware and you're worried about EMC. EM can cause corruption of registers, so by minimising the number of calls with goto statements, you can minimise risk of stack losses and the software failing to return from called routines.

View Tutorial          By: Jonathan H at 2012-05-10 08:46:48
2. View Comment

Norman, it would be good to credit where your write up above comes from. Its as originally published in the 2nd edition of K&R - The C programming Language

View Tutorial          By: K&R at 2014-03-20 11:41:56

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Norman Chap
The break Statement example in Java
Stored Procedures example in SQL
JDBC and Tomcat context settings
File read and write - sample program in C
Pointers and Function Arguments in C
Writing the first Struts application
goto and labels in C
External Variables and Scope in C
Multiple inheritance example in C++
qsort() sample program in C++
Image and ImageItem sample program in J2ME
SimpleDateFormat sample program in Java
Observable class and the sample program in Java

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Find square and square root for a given number in C
UNIX read and write system calls sample program in C
Using memset(), memcpy(), and memmove() in C
Open, Creat, Close, Unlink system calls sample program in C
lseek() sample program in C
Formatting with printf in C
Printing a simple histogram in C
Initialization of Variables in C
Listing Files and Directories sample program in C
Sum of the elements of an array in C
perror() Function - example program in C
assert() Function Example program in C
Conditional Expressions in C
Macro Substitution using #define in C
A Storage Allocator sample program in C
Most Emailed Articles (in C)
Open, Creat, Close, Unlink system calls sample program in C
The C Character Set
Using malloc() Function in C
perror() Function - example program in C
Simple arithmetic calculations in C
Increment and Decrement Operators in C
register Variables in C
Bitwise Logical Operators in C
Using qsort() and bsearch() with strings - example program in C
Formatting with printf in C
Printing a simple histogram in C
Relational and Logical Operators in C
While and For Loops in C
goto and labels in C
getch and ungetch in C