Type Conversion
Consider the following program

#include
#define YES 'Y'
#define NO 'N'

main( )
{ /* main */
int RawResponse;
char Response;
printf("Do you like programming? ( Y or N): ");
RawResponse = getchar( );
Response = RawResponse;
if ( Response = = YES )
printf( "Just wait until you can see what you can do!");
if ( Response = = NO ) {
printf( "Keep trying! I bet you will change your mind.\n");
printf(" Programming can be a blast!");
}
}
The above program has an unusual feature. A character is read using
getchar and it is assigned to RawResponse which is an
integer, not a character variable. Why?


getchar can return special codes that show a failure to successfully
read a character. These error codes cannot be represented by a character
variable. To see these codes, getchar's value must be assigned to an
integer variable, not a character.

Some program will check for these error codes because correct operation must be verified as the program proceeds. In our case, RawResponse is immediately reassigned to Response and the error code is dropped in the process.

Assigning RawResponse to Response changes the value of the variable from an integer to a character. C usually enables movement back and forth freely between integers and characters. C converts the type of the value of RawResponse to that of Response. This is a harmless case of type conversion; sometimes type conversion play havoc with programs.