私は次のクラスのために C のスキルを磨いていprintf
ますgetchar
。具体的には、出力しようとする文字列は、各文字に同じ文字シーケンスが追加されます。、および(Xcode)でコンパイルするようにfoo
なります。この問題を示すサンプル コードを次に示します。"f?8@{?o?8@{?o?8@{?"
cc
f¿:¿o¿:¿0¿:¿
Apple LLVM 5.0
char * input_buffer = malloc( sizeof( char ) );
char c;
while ( ( c = getchar() ) != '\n' ) {
strcat(input_buffer, &c);
}
// problem output
printf( "\n%s\n", input_buffer );
// foo -> f¿:¿o¿:¿0¿:¿
// weird side effect is the 4 is required to get a proper len
printf("\ncharacters: %lu\n", strlen( input_buffer ) / 4 );
私はどこでも検索しましたが、これは他のどこにも見られませんが、これは少し特殊なケースのようです. これは、私が考慮していないエンコードの問題ですか?