I wrote a program that was supposed to replace an tab in the input with '\\t', any backspace with '\\b' and the backslash itself was to be replaced with '\\\'. Here is what I wrote initially:
Code:
#include <stdio.h>
int main(void)
{
int c;
while( ( c = getchar() ) != EOF ){
if( ( c = '\t' ) ){
printf( "\\t" );
}
else if( ( c = '\b' ) ){
printf( "\\b" );
}
else if( ( c = '\\' ) ){
printf( "\\\\" );
}
else
putchar( c );
}
return 0;
}
Obviously it does not work the way I wanted it to. Then I wrote the same program using a switch statement:
Code:
#include <stdio.h>
int main(void)
{
int c;
while( ( c = getchar() ) != EOF ){
switch ( c ){
case '\t':
printf( "\\t" );
break;
case '\b':
printf( "\\b" );
break;
case '\\':
printf( "\\\\" );
break;
default:
putchar( c );
}
}
return 0;
}
The second version works correctly. Why does the first one not work? I know it will be something simple, but I am missing it totally.
~/