[{"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}]
很多同学在出识C语言时大多会遇到scanf不能使用的问题
先上解决方案!
方案一:在每一个scanf后面加_s scanf—>scanf_s
scanf_s
方案二:在项目第一行定义_CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS 1
方案三:一劳永逸,但需要一点技术。找到vs的拷贝文件把我们方案二的代码
直接放在拷贝文档里,以后每次建立新项目时就会自动添加这行代码(详情请看本人空间的:VS文件初始化教程)
现在我就来告诉大家为什么报错,以及如何解决。
1.
我们在写好代码后,ctrl+F5让代码运行
随之而来的就是一乱连串的报错哈哈,为什么会出现这中情况呢?我们来看一下输出分析
他说:scanf这个函数不安全,建议我们使用scanf_或者使用_CRT_SECURE_NO_WARNINGS
我们来测试一下
1. 加上_s
2. 定义_CRT_SECURE_NO_WARNINGS
都能使代码很好的完成。
这时候很多好奇的宝贝就会问了,为什么不安全呢,我们难道学了个不安全的函数?
我们通过测试代码来阐述它的不安全
#define _crt_secure_no_warnings 1 #include<stdio.h> int main() { char arr[5] =
{ 0 }; scanf("%s", arr); //这时候scanf就可能不安全了,arr定义的5个空间,而我们通过scanf可以输入很多个数字
//比如输入六个数字然后回车,会直接报错 //scanf是你给它多少它直接存多少,没有管空间是否能承受得住
//scanf_s是只有vs里才有的,其他编译器不易定能运行成功 //所以我们直接在第一行直接定义#define
_crt_secure_no_warnings 1消除报错 //就不用每次写scanf的时候都写成scanf_s return 0; }
我们定义的arr数组为5个空间,而我们通过scanf可以输入很多个数字
比如输入六个数字然后回车,会直接报错
scanf是你给它多少它直接存多少,没有管空间是否能承受得住
另外scanf_s是只有vs里才有的,其他编译器不易定能运行成功(不具有通性)
所以长远来看我们最好还是使用方案二,三的方法消除报错。