gpt4 book ai didi

c - 在代码块中出现 "implicit declaration of function"错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:13:22 26 4
gpt4 key购买 nike

我对 C 还是很陌生,我总是在代码块中遇到错误,这使我无法运行我的程序。我收到错误消息“函数 printf_s() 和 scanf_s() 的隐式声明。这是我的代码:

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>

int main(void)
{
int age = 0;
char name[20];

printf_s("Enter your age: ");
scanf_s("%d", &age);

print_s("Enter your name: ");
scanf_s("%s", name, sizeof(name));

printf_s("Your name is %s and you are %d years old.\n", name, age);

return 0;
}

最佳答案

printf_sscanf_s 仅在 __STDC_LIB_EXT1__ 由库实现定义时可用。它是从 C11 标准开始添加的。

首先你必须检查 __STDC_LIB_EXT1__ 是否被定义,然后你才应该使用 printf_sscanf_s

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
int main(void)
{
int age = 0;
char name[20];
#ifdef __STDC_LIB_EXT1__
printf_s("Enter your age: ");
scanf_s("%d", &age);
print_s("Enter your name: ");
scanf_s("%s", name, sizeof(name));
printf_s("Your name is %s and you are %d years old.\n", name, age);
#else
printf("Enter your age: ");
scanf("%d", &age);
print("Enter your name: ");
scanf("%19s", name);
printf("Your name is %s and you are %d years old.\n", name, age);
#endif
return 0;
}

关于c - 在代码块中出现 "implicit declaration of function"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37427267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com