- 论坛徽章:
- 0
|
1.问题:在第一个程序中,加上buffer[x++] = 0的作用是为了使得printf 函数能够知道输出的结束位点,是吧?那么,为何将其删除后,程序依然可以运行?将你的输入在下一行输出。
2.在第二个程序中,为何删除buffer[x++]=0后,在当你无回显输入后,输出你输入的字符,后面接一些乱码?
这个和内存的初始化有关系吗?
下面是程序:
/* Using getchar to input strings. */
#include "stdio.h"
#define MAX 80
int main( void )
{
char ch, buffer[MAX+1];
int x = 0;
while ((ch = getchar()) != '\n' && x < MAX)
buffer[x++] = ch;
buffer[x++] = 0;
printf("%s\n", buffer);
return 0;
}
-----------------------------
/* Using getch to input strings. */
#include "stdio.h"
#define MAX 80
int main( void )
{
char ch, buffer[MAX+1];
int x = 0;
while ((ch = getchar()) != '\r' && x < MAX)
buffer[x++] = ch;
buffer[x++] = 0;
printf("%s\n", buffer);
return 0;
}
------------------ |
|