输出结果的两行按理说是一样的,怎么会多出一个换行符?
#include "stdio.h"#define STR_LEN 10
int read_line(char str[], int n);
main()
{
int i, n;
char str[STR_LEN+1];
n = read_line(str, STR_LEN+1);
for(i = 0; i <= n; i++)
{
printf("%c", str[i]);
}
printf("asd\n");
puts(str);
printf("asd\n");
}
int read_line(char str[], int n)
{
int i = 0;
char ch;
while((ch = getchar()) != '\n')
if(i < n - 1)
str[i++] = ch;
str[i] = '\0';
return i;
}
[ 本帖最后由 我爱吃青菜 于 2015-3-29 21:22 编辑 ]