- 论坛徽章:
- 0
|
这是我的源程序。
程序很简单,通过对test值的判断,当test值为'n'时,跳出循环,程序退出;
当test的值为非'n'时,输出"Do this!"循环3次。
但运行结果却很让人奇怪……
1 #include <stdio.h>
2
3 int main(void){
4 int hcount=0;
5 char test='\0';
6
7 for(hcount=0; hcount<3; hcount++)
8 {
9 printf("\nDo you want to enter details of a%s horse (Y or N) ? ",
10 hcount? "nother": "");
11 scanf("%c",&test);
12 if(test == 'n')
13 break;
14 printf("\n\nDo this !\n");
15 }
16 return 0;
17 }
18
|
以下是运行结果:
flo@linux-icyw:~/bin/c> gcc -Wall -g test.c -o test
flo@linux-icyw:~/bin/c> ./test
Do you want to enter details of a horse (Y or N) ? y
Do this !
Do you want to enter details of another horse (Y or N) ?
Do this !
Do you want to enter details of another horse (Y or N) ? y
Do this !
|
注意绿色的一行!!!她并没有询问我的输入,自己竟然主观的判断了test的取值非'n'。…………
我用gdb调试时,在第二次循环(如果有的话)中,test被自动赋值为'\n'!这应该就是在运行结果中,出现绿色一行的原因。
请大家解释下,为什么test会在第二次循环中被自动赋值为'\n'呢??? |
|