Confusing Character Constants and Character Strings

Remember the difference between a character constant, which has one byte, and a character string, which is a series of characters ending with a null character:

char ch = 'Y';

if( ch == "Y" ) /* Error! */

printf( "The ayes have it..." );

The example above mistakenly compares the char variable ch to a two-character string ( "Y" ) instead of a single character constant ( 'Y' ). Since the comparison is false, the printf statement never executes—no matter what ch equals.

The if statement needs to use single quotes. This code correctly tests whether ch
equals the character 'Y':

char ch = 'Y';

if( ch == 'Y' )

printf( "The ayes have it..." );