1 /************************************************************************* 2 > File Name: goto.c 3 > Author: Mr.Yang 4 > Purpose:演示goto的用法 (段错误出现的场景) 5 > Created Time: 2017年05月19日 星期五 18时36分23秒 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 int main(void) 12 { 13 int i = 0; 14 int n = 0; 15 start: 16 17 for(i = 0;i<10;i++) 18 { 19 printf("enter a number between 0 and 10:"); 20 scanf("%d",&n);//当接收用户输入时,scanf函数中未加"&"符号,编译能通过,但是会出现“段错误”这样的错误提示 21 if(n >10 || n < 0) 22 { 23 goto start; 24 } 25 else if(n == 0 ) 26 { 27 goto location0; 28 } 29 else if(n == 1) 30 { 31 goto location1; 32 } 33 else 34 { 35 goto location2; 36 } 37 } 38 39 location0: 40 printf("you entered %d\n",n); 41 goto end; 42 43 location1: 44 printf("you entered %d\n",n); 45 goto end; 46 47 location2: 48 printf("you entered between 2 and 10\n"); 49 50 end: 51 52 return 0; 53 }
时间: 2024-11-12 21:16:56