【发布时间】:2014-11-07 06:21:58
【问题描述】:
我浪费了好几个小时来思考为什么这个程序运行不正常,没有成功。它总是打印“字符是特殊符号”。
#include <stdio.h>
int main( void )
{
char character;
printf( "Please type any character and I will tell you what type it is:\n" );
while( 1 )
{
scanf( "%c", &character);
if( character >= 65 && character <= 90 )
printf( "The character is A-Z\n" );
else if( character >= 97 && character <= 122 )
printf( "The character is a-z\n" );
else if( character >= 48 && character <= 57 )
printf( "The character is 0-9\n" );
else if(( character >= 0 && character <= 47 ) ||
( character >= 58 && character <= 64 ) ||
( character >= 91 && character <= 96 ) ||
( character >= 123 && character <= 127 ))
printf( "The character is a special symbol\n" );
}
}
运行示例
Please type any character and I will tell you what type it is:
4
The character is 0-9
The character is a special symbol
我注意到当我删除 while 循环时它不会发生,但我不明白为什么,我想要那个循环。
【问题讨论】:
-
不要使用带有字符的scanf,而是使用fgetc(stdin)
-
因为它的工作方式丑陋。 fgetc(stdin) 不受前导空白字符(如 \n)的影响。当然它仍然可以得到它们,但没有转换。 stackoverflow.com/questions/12063879/…
标签: c if-statement while-loop