Goto Statememt Print Text in C


This is a C program that prompts the user to enter an integer 'n' and then uses the goto statement to print the string "Hi" 'n' times.

  • The #include<stdio.h> is a preprocessor directive that includes the contents of the standard input-output library in the program.
  • The int main() function is the starting point of the program execution. Inside the main function, an integer n and a counter i is declared and initialized to 1.
  • printf() is used to display the prompt for the user to enter the limit, and scanf() is used to get the input and store it in the variable 'n'.
  • A label start is defined and a goto statement is used to jump to this label. The code between the label and the goto statement will execute repeatedly until the condition i<=n is false.
  • The first time the code is executed, the string "Hi" is printed and the value of i is incremented by 1. Then the condition i<=n is checked, if it's true the control jumps back to the label 'start' and the same process is repeated until the condition is false.
  • It is worth noting that the use of the goto statement is generally discouraged in modern programming because it can lead to unstructured and hard-to-maintain code.
  • Alternative control structures such as for loops, while loops or do-while loops can be used for the same purpose and are considered more readable, maintainable and less prone to errors.

Finally, the return 0 statement is used to indicate the successful execution of the program. The return value of 0 is a convention used to indicate that the program has executed correctly.

Source Code

#include <stdio.h>
int main()
{
  int i=1,n;
  printf("Enter The Limit:");
  scanf("%d",&n);
  start:
  printf("\nHi");
  i++;
  if(i<=n)
  {
    goto start;
  }
  return 0;
}
To download raw file Click Here

Output

Enter The Limit : 5
Hi
Hi
Hi
Hi
Hi

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C