scanf函数


#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;
}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值