C – goto statement

goto: A branching statement used to send the control from one place to another place in the program by defining labels.

Program to display 1 to 10 numbers without loop:

#include<stdio.h>
int main(){
            int a=1;
            top:
                        printf(“a value : %d \n”, a);
                        ++a;
                        if(a<=10)
                                    goto top;
            return 0;          
}

We can create multiple labels in a single program.

#include<stdio.h>
int main()
{
            top:
                        printf(“Top \t”);
                        goto bottom;
            center:
                        printf(“Center \t”);
            bottom:
                        printf(“Bottom \n”);
                        goto top;        
            return 0;          
}

We use goto statement to come out of infinite loop also.

#include<stdio.h>
int main()
{
            while(1)
            {
                        printf(“Infinite loop \n”);
                        goto end;
            }
            end:
                        printf(“Can break with goto\n”);         
            return 0;          
}
Scroll to Top