免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 网路幽灵
打印 上一主题 下一主题

有关break产生的疑惑!!! [复制链接]

论坛徽章:
0
11 [报告]
发表于 2005-05-07 16:02 |只看该作者

有关break产生的疑惑!!!

不能接受字符输入我能理解,为什么第一段代码我输入三个数后输入a(输入不正确)这时应该因为输入格式不对而终止,m也应该是终止时的值吧?怎么m会是100呢?这100又是怎么得到的呢???

输入格式不对计算机不读入值,并非意味着程序就此停住。循环会继续执行,但是每次都是读到'a'(因为'a'被存储在输入缓冲区中,scanf()从中取值)由于格式不匹配,所以每次都不能成功读入,程序就这样一值执行到m=100为止。

试试将
  1. for(m=0;m<100;m++){
  2.       scanf("%d",&a[m]);
  3.       if(a[m]=='a')
  4.          break;
  5.    }
复制代码

改成
  1. for(m=0;m<100;m++){
  2.       ret = scanf("%d",&a[m]);          //add
  3.       printf("ret(%d) = %d", m, ret); //add
  4.       if(a[m]=='a')
  5.          break;
  6.    }
复制代码

将会看到在输入'a'字符后所有的ret(m)=0,说明scanf()未读入值。但是能打印出所有100个ret值说明循环在执行!

论坛徽章:
0
12 [报告]
发表于 2005-05-07 21:23 |只看该作者

有关break产生的疑惑!!!

如果scanf不读入,那为什么我加上的printf会有我输入的数打印出来呢???
main()
{static int a[100];
int m,n;
for(m=0;m<100;m++)
   {scanf("%d",&a[m]);
     if(a[m]=='a')
     break;}  
for(n=0;n<m;n++)
printf("%5d",a[n]);
}

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
13 [报告]
发表于 2005-05-07 21:30 |只看该作者

有关break产生的疑惑!!!

>;>;如果scanf不读入

是什么意思?

论坛徽章:
0
14 [报告]
发表于 2005-05-08 07:29 |只看该作者

有关break产生的疑惑!!!

原帖由 "mq110" 发表:
>;>;如果scanf不读入

是什么意思?

看过上面的帖子了吗,是kernelxu说输入格式错误scanf就不读入值!

论坛徽章:
0
15 [报告]
发表于 2005-05-08 10:36 |只看该作者

有关break产生的疑惑!!!

如果scanf不读入,那为什么我加上的printf会有我输入的数打印出来呢???

假设你输入:
12
21
12
a
结果应该是:
12 21 12 0 0 0 0 ……0
对吧?那都是你输入的数吗??至少输不出'a'或97吧!!
说明scanf()并没有成功地将'a'赋给数组a[m]的某一元素!
我说过程序会继续执行,遇见你的打印命令当然会执行,只不过它打印不出你输入'a'及其以后的数,而是打印你定义a[m](static in a[m])时系统自动给a[m]赋的初值0!
我强调的只是scanf()未将缓冲区中的'a'成功地赋给a[m],而程序却在继续执行直到m=100为止。所以m=100。

论坛徽章:
0
16 [报告]
发表于 2005-05-08 12:12 |只看该作者

有关break产生的疑惑!!!

谢谢,明白了!

论坛徽章:
0
17 [报告]
发表于 2005-05-08 18:59 |只看该作者

有关break产生的疑惑!!!

3.7 Break and Continue
It is sometimes convenient to be able to exit from a loop other than by testing at the top or bottom. The break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost enclosing loop or switch to be exited immediately.
The following function, trim, removes trailing blanks, tabs and newlines from the end of a string, using a break to exit from a loop when the rightmost non-blank, non-tab, non-newline is found.

   /* trim:  remove trailing blanks, tabs, newlines */
   int trim(char s[])
   {
       int n;

       for (n = strlen(s)-1; n >;= 0; n--)
           if (s[n] != ' ' && s[n] != '\t' && s[n] != '\n')
               break;
       s[n+1] = '\0';
       return n;
   }

strlen returns the length of the string. The for loop starts at the end and scans backwards looking for the first character that is not a blank or tab or newline. The loop is broken when one is found, or when n becomes negative (that is, when the entire string has been scanned). You should verify that this is correct behavior even when the string is empty or contains only white space characters.
The continue statement is related to break, but less often used; it causes the next iteration of the enclosing for, while, or do loop to begin. In the while and do, this means that the test part is executed immediately; in the for, control passes to the increment step. The continue statement applies only to loops, not to switch. A continue inside a switch inside a loop causes the next loop iteration.

As an example, this fragment processes only the non-negative elements in the array a; negative values are skipped.

   for (i = 0; i < n; i++)
       if (a < 0)   /* skip negative elements */
           continue;
       ... /* do positive elements */

The continue statement is often used when the part of the loop that follows is complicated, so that reversing a test and indenting another level would nest the program too deeply.

论坛徽章:
0
18 [报告]
发表于 2005-05-08 19:26 |只看该作者

有关break产生的疑惑!!!

[root@mrtg-2 root]# more test.c
main(){
   static int a[100];
   int m,n;
   for(m=0;m<100;m++){
      scanf("%d",&a[m]);
      if(a[m]=='a')
         break;
   }
   printf("output:%d\n",m);
}

[root@mrtg-2 root]# ./a.out
11
22
123
456
97
output:4
[root@mrtg-2 root]#

论坛徽章:
0
19 [报告]
发表于 2005-05-08 20:14 |只看该作者

有关break产生的疑惑!!!

又弄清楚一个细节!
拜谢楼上诸位提出问题和解答问的CUer!

论坛徽章:
0
20 [报告]
发表于 2005-05-08 21:12 |只看该作者

有关break产生的疑惑!!!

没仔细看 上面的贴
我个人觉得取地址
好像应该是这个样子取才对
&(a[m])
感觉&a[m]这样的话
总是取的a[0]的地址
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP