#include<stdio.h>
int main(void){
int a;
char c;
printf("%d\n",'\r');
while(c!='N')
{
//scanf("%d",&a);
scanf("%c",&c);
if('\n'==c)continue;
printf("a=%dc=%c\n",a,c);/*printf("c=%d\n",c);*/
}
return 0;
}
通过这个函数可以确定scanf函数可以接受回车作为输入。
对于为什么要用&,作为函数只有利用地址来完成数据的赋值。
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
空格控制符
#include<stdio.h>
int main(void)
{
int i;
char j;
for(i=0;i<10;++i)
scanf(" %c",&j);/*这里%前空格*/
printf("%c",j);
return 0;
}
在标记处%前加一个空格即可,可以略去输入字符串中的空格字符
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i;
char* j=(char*)malloc(100);
scanf(" %s",j);
printf("%s",j);
free(j);
j=NULL;
return 0;
}
当输入hello world时,只会输出hello
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i;
char* j=(char*)malloc(100);
scanf(" %s",j);
printf("%s",j);
fgets(j,7,stdin);//此处用gets(j)也可
printf("%s",j);
free(j);
j=NULL;
return 0;
}
此时输入hello world 则会输出hello world,可以通过文件操作函数来对stdin进行操作,
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i;
char* j=(char*)malloc(100);
scanf(" %s",j);
printf("%s\n",j);
fgets(j,2,stdin);//从stdin中获取一个字符
printf("%s\n",j);
fputs(j,stdin);//次操作会将stdin中的前面sizeof(j)个字符抹掉
gets(j);
printf("%s\n",j);
free(j);
j=NULL;
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i;
int m=4;
char* j=(char*)malloc(100);
scanf(" %s",j);
printf("%s\n",j);
fgets(j,m,stdin);
printf("%s\n",j);
for(int k=m-2;k>=0;k--)
{
ungetc(j[k],stdin);//使用ungetc可以恢复stdin中的内容
}
gets(j);
printf("%s\n",j);
free(j);
j=NULL;
return 0;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、