gpt4 book ai didi

c - 为什么 strcpy() 在不包含 的情况下工作

转载 作者:太空宇宙 更新时间:2023-11-04 05:16:00 27 4
gpt4 key购买 nike

我正在处理一些 C 示例,并使用函数 strcpy() , 但忘了包括 <string.h> , 虽然我已经包含了 <stdio.h> .令我惊讶的是,代码运行成功。以下是我正在执行的代码:

#include <stdio.h>

int main() {
char message[10];
int count, i;

strcpy(message, "Hello, world!");

printf("Repeat how many times? ");
scanf("%d", &count);

for(i=0; i < count; i++) {
printf("%3d - %s\n", i, message);
}
}

我正在使用 gcc version 3.3.6 (Ubuntu 1:3.3.6 - 15ubuntu1)

我什至没有收到任何编译警告。

enter image description here

为什么我的代码在不包含 <string.h> 的情况下运行? ?

提前致谢。

最佳答案

编译器假定 int strcpy() 并调用古老的向后兼容行为。指针参数是它适用的类型之一,所以没问题。

不要依赖它。安全栏杆已关闭。

关于c - 为什么 strcpy() 在不包含 <string.h> 的情况下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55477097/

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