goto statement in C Language

goto statement in C programming language is used for unconditional jump from one part of the program to another part of the program. It is always suggested not to use goto statement as this reduces the readability of the program. Using goto statement in C programming language is considered as poor programming approach.

The goto statement consists of two parts: label and goto keyword.

Example:

/*use of goto statement*/
#include<stdio.h.>
#include<conio.h>
void main(){
    int a;
    goto label;
    a = 10;
    printf(ā€œ%dā€, a);
    label:
    a = 20;
    printf(ā€œ%dā€, a);
}

Output:
20

When goto label is encountered, control goes to the statement next to the label. Here, label is not the keyword. We can use any name for the label. It is user defined.

You may also like...

16 Responses

  1. souvik says:

    what is the alternative approach then?

  2. fahad baig says:

    can we use conditions with goto ?
    like goto label if (a=10)
    ?

  3. Anonymous says:

    You should be able to use..

    if (a == 10){
    goto label;
    }

  4. Anonymous says:

    respected sir or madam would like to give another example using goto statement,to understand its usage clearly and properly.please.please.please.please reply at your earliest possible.

    • srikanth says:

      #include
      #include
      void main()
      {
      int i;
      label1:
      printf(“\n\n\t Enter the value of i:- “);
      scanf(“%d”,&i);
      if(i>0)
      {
      printf(“\n\n\t Thank you for positive value…,”);
      goto label2;
      }
      else
      {
      printf(“\n\n\t Only Positive Numbers Pls..,”);
      goto label1;
      }
      label2:
      printf(“\n\n\t World didn’t end here…,”);
      getch();
      }

  5. Anonymous says:

    what does these statements mean?

    #include
    #include

    • ansh pethe says:

      The #include preprocessor directive is used to paste code of given file into current file. It is used include system-defined and user-defined header files. If included file is not found, compiler renders error. By the use of #include directive, we provide information to the preprocessor where to look for the header files.

  6. Vishal says:

    #include are preprocessors
    means they contain predefined functions..which are later used in program:)

  7. Anonymous says:

    please give another example for using goto in more complex way..like loops in loops

  8. Anonymous says:

    #include are the preprocessors that will tell the compiler to include the text in the file STDIO.H before it executes its source code.

  9. LearnCOnline says:

    Thanks readers for your valuable comments.

    In addition, We have recently started a new forum.
    http://forum.learnconline.com/

    You can post your queries and get it resolved.

    Thanks,
    LearnCOnline.com

  10. Learning becomes easy here..thank you

  11. Anonymous says:

    ThaNk yew sooo mucH. This site iz very Helpful 4 us to leaRn moRe about C:)

  12. Anonymous says:

    great site

  13. Mahendra says:

    Sir, how to decide which text file should be include in pre-processor directive ?

Leave a Reply

Your email address will not be published. Required fields are marked *

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet