|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 1613551 于 2022-4-21 16:24 编辑
有什么办法可以不用在函数后面加上_s也可以正常编译吗?
比如下面这个程序一定要在第9行strcpy后面加上_s才行
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char str1[100] = "Original";
- char str2[10] = "New";
- strcpy_s(str1, str2);
- printf("%s\n", str1);
- return 0;
- }
复制代码
如果不加,像是下面这样,那么程序就会报错,为什么dev c++不会?
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char str1[100] = "Original";
- char str2[10] = "New";
- strcpy(str1, str2);
- printf("%s\n", str1);
- return 0;
- }
复制代码
不止是strcpy这样,包括scanf也是这样,要在第8行和第10行加上_s才行
- #include <stdio.h>
- int main() {
- int a, b, c;
- int d, e, f;
- int h = 0;
- printf("请输入你的生日(如果1988-05-20):");
- scanf_s("%d-%d-%d", &a, &b, &c);
- printf("请输入今年的日期(如2016-03-28):");
- scanf_s("%d-%d-%d", &d, &e, &f);
- for (; a <= d; a++) {
- int g[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- g[1] = (a % 400 == 0 || (a % 4 == 0 & a % 100 == 0))? 29 : 28;
- for (; b <= 12; b++) {
- for (; c <= g[b - 1]; c++) {
- if (a == d && b == e && c == f) {
- goto result;
- }
- h++;
- }
- c = 1;
- }
- b = 1;
- }
- result:
- printf("你在这个世界上总共生存了%d天\n", h);
- return 0;
- }
复制代码
如果不加,像是下面这样,那么就会报错
- #include <stdio.h>
- int main() {
- int a, b, c;
- int d, e, f;
- int h = 0;
- printf("请输入你的生日(如果1988-05-20):");
- scanf("%d-%d-%d", &a, &b, &c);
- printf("请输入今年的日期(如2016-03-28):");
- scanf("%d-%d-%d", &d, &e, &f);
- for (; a <= d; a++) {
- int g[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- g[1] = (a % 400 == 0 || (a % 4 == 0 & a % 100 == 0))? 29 : 28;
- for (; b <= 12; b++) {
- for (; c <= g[b - 1]; c++) {
- if (a == d && b == e && c == f) {
- goto result;
- }
- h++;
- }
- c = 1;
- }
- b = 1;
- }
- result:
- printf("你在这个世界上总共生存了%d天\n", h);
- return 0;
- }
复制代码
微软编译器报错机制,如果不要加 s,想用scanf()可以试试在代码最上面(也就是第一行)加上 #define _CRT_SECURE_NO_WARNINGS
|
|