69,369
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
char s[100];
scanf("%s\n", s);
printf("%s", s);
system("pause");
return 0;
}
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
char *s = NULL;
scanf("%s\n", s);
printf("%s", s);
system("pause");
return 0;
}
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
char *s = "";
scanf("%s\n", s);
printf("%s", s);
system("pause");
return 0;
}
char ch,a[20];
fscanf(stdin,"%s",a);
fscanf(stdin,"%c",&ch);
用户输入空格\n空格\n然后abcdef\n这就结束了。这时a的字符串就是abcdef,前面的空格\n空格\n被吸收了。如果是%d同样也会吸收。然后他读到abcdef\n,发现\n结束流,然后匹配到\n,把\n留下,%c读到\n马上结束。
char ch,a[20];
gets(a);
fscanf(stdin,"%c",&ch);
但对于这个你输入abcdefg\n他是不会结束的,因为\n被消耗掉,所以你输入\n\n这种就会结束,
这时赋给a的就是a[0]='\0',给c的却是10(\n)。
int _tmain(int argc, _TCHAR* argv[])
{
char *s ;
s=(char *)malloc(100);
scanf("%s\n", s);
printf("%s", s);
system("pause");
return 0;
}
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
char s[100];
//scanf("%s\n", s);
fgets(s, 99, stdin);
printf("%s", s);
system("pause");
return 0;
}
后两种爆出异常的原因是没有给s分配空间。