[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
scanf的返回值是正确输入的参数个数。
scanf()函数接收输入数据时,遇以下情况结束一个数据的输入:
(1)遇空格、“回车”、“跳格”键;
(2)遇宽度结束;
(3)遇非法输入。
以下这段代码:
//vs可自定义加注释(在选项中更改),我的是ctrl+q,可选中多行加注释,alt+回退键=撤销 #include<stdio.h> int main()
{ char c[20]; printf("请输入一串英文:"); scanf_s("%s", c,20);
//加上不超过数组的大小才能正常输出且不能超过数组长度 printf("输入的字符为:%s\n", c); return 0; }
遇到空格则判定输入结束,后续不再接收:
只能输入不带空格的一串字符,且输入的字符串超出定义的参数长度,则无法正常输出:
scanf_s(“%s”, c,10);
超过数组定义的长度,会直接出弹窗警告:
解决方法1:定义多个数组用于存储字符串
//因为scanf遇到空格就认为输入结束,因此,想要输出i am student,可以试试定义三个字符串: #include <stdio.h> int
main() { char str1[10], str2[10], str3[10]; printf("请输入字符串:"); scanf_s("%s%s%s",
str1,10, str2,10, str3,10); printf("输出结果:%s %s %s\n", str1, str2, str3);
//%s间要加空格 return 0; }
方法2:%[]的格式控制法:%[scanfset]
scanfset有两种形式:
一种是以非“^”字符开头的scanset ,表示在读入字符串时将匹配所有在scanfset中出现的字符,遇到非scanfset中的字符时输入就结束;
一种形式是以“^“字符开头的scanfset,表示在读入字符串时将匹配所有不在scanfset中出现的字符,遇到scanfset中的字符输入就结束。
上面的代码经过处理后就可以实现,字符串中带有空格输入:
#include<stdio.h> int main() { char str[80]; scanf_s("%[^\n]", str,80);
//记得给scanf_s指定一个参数(范围) printf("%s\n", str); return 0; }
如果想用%c输出带空格的字符,只能利用循环逐个输出:
#include<stdio.h> int main() { char c[20]="i am student."; for (int i = 0; i <
20; i++) { printf("%c", c[i]); } printf("\n"); return 0; }
用scanf函数输入,判断输入字符长度,决定终止位置。
//我自己写的,非完美答案 #include<stdio.h> #include <string.h> int main() { char c[20];
int i = 0; int strLen=0; scanf_s("%[^\n]", c, 20); strLen = strlen(c); for (int
i= 0; i < strLen; i++) { //获取输入的字符串长度 if(c[i]!='\n') //不回车,就认为还在输入,避免空格影响判断
printf("%c", c[i]); } printf("\n"); return 0; }
字符%c输出和字符串%s输出是有区别的,%c会输出定义的数组长度的字符,也就是如果输入长度小于定义数组长度,后面会补上一些乱七八糟的字符填充够数组的长度;
类似这样:
%s是遇到‘\n’则知道终止位置,就会停止输出。