为什么要包括stdlib.h中? [英] Why to include stdlib.h?
问题描述
C函数的malloc()是在stdlib.h中定义
它应该给错误如果不包括此文件,但这个code正常工作与一个小警告。我的问题是,如果malloc()函数工作没有这个头文件,那么为什么把它列入?请清除我的概念。
#包括LT&;&stdio.h中GT;诠释的main()
{
诠释A,B,* P;
P =(INT *)malloc的(的sizeof(int)的* 5);
用于:(a = 0;一个小于5;一个++)P [α] = A * 9;
为(B = 0; B&小于5; b ++)输出(%d个,第并[b]);
}
在C不幸的是,你不需要pre-声明功能。如果编译器以一个新的功能,遇到它会为它创建一个隐含的声明(mmm`kay,正是这个怎么用,所以我会假设的参数的类型是..)。
不要依赖这个功能和一般不写code与警告编译。
C function malloc() is defined under stdlib.h It should give error if we don't include this file but this code works fine with a little warning. My question is if malloc() works without this header file then why to include it? Please clear my concepts.
# include <stdio.h>
int main()
{
int a, b, *p;
p = (int*)malloc(sizeof(int)*5);
for(a=0;a<5;a++)p[a]=a*9;
for(b=0;b<5;b++)printf("%d ",p[b]);
}
In C unfortunately you don't need pre-declaration for functions. If the compiler encounters with a new function it will create an implicit declaration for it ("mmm`kay, this how it is used so I will assume that the type of the arguments are..").
Do not rely on this "feature" and in general do not write code that compiles with warnings.
这篇关于为什么要包括stdlib.h中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!