而使用“if”的循环不能正常工作

时间:2014-09-13 01:33:30

标签: c if-statement while-loop

我浪费时间思考为什么这个程序运行不正常,没有成功。它总是打印“角色是一个特殊的符号”。

#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循环时没有发生,但我不明白为什么,我想要那个循环。

2 个答案:

答案 0 :(得分:6)

您的scanf应该是这样的:

    scanf(" %c", &character);

您收到The character is a special symbol因为scanf也在阅读\n

答案 1 :(得分:-3)

4符合printf的条件(“该字符是特殊符号\ n”);