这是一个程序,如果输入的数量小于 72,则采用 std 输入并显示以换行符结尾的 72 个字符,它必须附加终止空字符,如果我打印缓冲区,我会得到垃圾值。这里有什么问题
int main() {
int buffer[ MAX_CHAR ];
int count = 0, ch = 0, i = 0;
while( (ch = getchar() != '@') ) {
// Buffer used to collect the i/p characters from the std i/p
buffer[count++] = ch;
if( MAX_CHAR - 1 == count ) {
buffer[ MAX_CHAR + 1 ] = '\n';
for(i = 0; i < MAX_CHAR; i++)
printf("%c", buffer[i]);
count = 0;
buffer[ MAX_CHAR ] = 0;
}
}
//If the input character is less than 72, then append the rest with spaces
if( count != MAX_CHAR - 1) {
for(i = count; i < MAX_CHAR - 1; i++)
buffer[ count ] = ' ';
buffer[ MAX_CHAR ] = '\n';
for(i = 0; i < MAX_CHAR; i++)
printf("%c", buffer[i]);
}
}