Nested Conditional Operators in C -


this question has answer here:

i having little trouble working nested conditional operators in c.

int is_correct() {    char yn ;    printf( "y or n : " ) ;   scanf( "%c", &yn ) ;   yn = toupper( yn ) ;    return ( yn == 'y' )? 1 : ( yn == 'n' )? 0 : is_correct() ; }      

i under impression last line of code return 1 or 0 if 'y' or 'n' entered or call again if unexpected character entered. instead continuously calls no matter input.

probably scanning failing, , you're not verifying it.

you don't specify if "continuously" means "without stopping read more input", of course should do.

note, instance, toupper() uses int-typed argument , results, , expecting values of type unsigned char might hit undefined behavior there.

this confusing aspect of ctype.h functions. tend cast unsigned char in call, if data comes text (char) buffer.

add printf() call print out value of yn before final line.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -