c - 带有 "if"的循环无法正常工作

标签 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 循环时它不会发生,但我不明白为什么,我想要那个循环。

最佳答案

你的 scanf 应该是这样的:

    scanf(" %c", &character);

你得到 The character is a special symbol 因为 scanf 也在读取 \n

关于c - 带有 "if"的循环无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25819002/

相关文章:

c - wait() 函数是否在处理 SIGCHLD 之前返回?

.net - 编译 C 以使用 CLR

java - 使用数组删除for循环中的最后一个逗号

excel - 每日总金额计算公式

java - 循环 while 和 if 语句优先级

c - 有效地对结构数组进行排序

Leetcode "Two Sums"的C解决方案有运行时错误

javascript - 如何在 javascript 中的 if 子句中区分输入值或 td 文本内容

java - 如何在java中仅使用while循环打印带有星号的正方形?

Java - 如何结束 while 函数的进程