×

Goto and Labels in C

Introduction

Goto in C:

The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto statement has one more application, which is, for a particular condition it performs repeat operation on some part of the code.

Labels in C:

The Labels in C are used with goto and switch statements. It can be defined as an identifier which is required for the goto statement. It is required as an identifier to a location where the branch is to be made.

Syntax:

goto label;

Theory:

The awareness about “goto” and “labels'' is widely known to every programmer who has gone through the surface of C programming. It is used in C programming to perform the jump operation. By jump we mean, jump within a C function.

Note:  In C, the GCC (GNU Compiler Collection) provides an extension which is called “local labels”.

Goto Statement in C

The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. C offers the infinitely-abusable goto statement, and labels to branch to. This means that the label is required as an identifier to a location where the branch is to be made.

The goto statement is not necessary, and in general it is quite easy to write code without the use of this statement. But there are a few scenarios where goto may play a vital role and resolve the problems the user may face in those conditions.

The most common scenario is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once.

Note: Here, the break statement may resolve the issue but one must remember that it cannot be used directly since it only exits from the innermost loop.

      for ( ... )
           for ( ... ) {
               ...
               if (condition_causing_disaster)
                   goto error;
           }
       ...
   error: error_statement

In the above lines of code, the former suggested organization is compatible if the error-handling code is non-trivial, and if the occurrence of errors is bound to be in several places.

Label Statement in C

A Label in C is used with goto and switch statements. A label is followed by a colon and what makes it special is that it has the same form as a variable name. Also,it can be adjoined with any statement in the same function as the goto. The label differs from the other statements of its kind is the scope. The scope of a label is the entire function.

Application

Example 1:   

   for (i = 0; i < n; i++)
           for (j = 0; j < m; j++)
               if (x[i] == y[j])


                   goto found;  //no common element found
       …


   found:
       /* found one common element : x [i] == y [j] */
       ...

Code explanation:

The above code is an attempt to solve the problem of determining whether two arrays x and y have an element in common. But this statement can be written in a more elegant way or we can say the easy approach. The above code uses the goto statement to solve the given condition.

But the code consisting of a goto statement can always be written without using this statement. It may cause the programmer trouble with repeated tests (testing conditions) or an extra variable.

Example 2:

  found = 0;
   for (i = 0; i < n && !found; i++)
       for (j = 0; j < m && !found; j++)


           if (x [i] == y [j])
               found = 1;
   if (found)
       // found one common element : x [i] == y [j]
       ...
   else
       // didn't find any common element 
       ...

It is commonly observed that the code that relies on goto statements is generally harder to understand. Not only is it harder to decode but also to maintain as compared to code without gotos.

Conclusion

The goto and label statements do come with its own advantages and the programmer tasks by reducing the number of lines of code (eg: reducing the number of testing conditions), but it is also true that they make the program quite hard to understand and decode.


Related Topics

How to open a C file on android mobile

A C file is a file that has a .c extension and contains code written in the C language. C is a computer programming language developed by Dennis Ritchie at...

4 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

C/C++ Program to Find the Size of int, float, double and char

In this tutorial, we will learn how to use the sizeof operator to determine the size of each variable. Program to Determine Variable Size Write a C or C++ program to determine the...

2 minutes read.

Infix to Postfix program in C

Infix Expression: In infix expression, an operator is placed between the two operands. Example: x + y, here operator + is placed between operands x and y. Postfix Expression: In...

3 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

SJF Scheduling Program in C

The SJF (shortest job first) or the shortest job next is the programming scheduling in the C. It is one of the CPU scheduling programming. The SJF is defined as...

4 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

OpenGL in C

OpenGL stands for Open Graphics Library. It is a low-level, widely supported modeling and rendering software package that is widely available across all the platforms. It is an API used...

4 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

Call by Value and Call by Reference in C

Call by reference and call by value are two different ways of passing arguments to a function in C programming language. Call by reference means that the called function is...

4 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

4 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.