Although the if loop is not working properly

I spent a lot of time thinking about why this program is dishonest, without success. He always prints "The symbol is a special symbol."

#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 AZ\n" ); else if( character >= 97 && character <= 122 ) printf( "The character is az\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" ); } } 

RUN example

 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 

I noticed that this does not happen when I delete the while loop, but I don’t understand why, I want this loop.

+5
source share
2 answers

Your scanf should look like this:

  scanf(" %c", &character); 

You get The character is a special symbol because scanf also reads \n .

+6
source

4 meets the condition printf ("The character is a special character \ n");

-3
source

Source: https://habr.com/ru/post/1202462/


All Articles