×

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

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

Variables in C

A variable can be defined as a name allocated to a storage space that can be manipulated by our programs. Every variable in C arbitrates about the overall layout and...

5 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 minutes read.

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

2 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

C program to find factorial of a number using Recursion

What does the term "Factorial of a Number" mean? In mathematics, factorial is the product of all positive integers that are less than or equal to a certain positive integer, and...

2 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Decimal to Binary in C

What is a decimal number? A decimal number is a number represented in the decimal number system. This system of binary conversion uses base 10 to represent numbers, i.e. the digits...

3 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.