简体   繁体   English

为什么我们要包含 stdlib.h?

[英]Why do we include stdlib.h?

C function malloc() is defined under stdlib.h . C function malloc()stdlib.h下定义。

It should give an 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 do we need to include it?我的问题是,如果malloc()在没有这个 header 文件的情况下工作,那么我们为什么需要包含它? Please help clarify 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.不幸的是,在 C 中,您不需要预先声明功能。 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..").如果编译器遇到新的 function 它将为其创建一个隐式声明(“嗯,这是它的使用方式,所以我将假设 arguments 的类型是......”)。

Do not rely on this "feature" and in general do not write code that compiles with warnings.不要依赖这个“特性”,并且通常不要编写编译时带有警告的代码。

Read the warning.阅读警告。 It says it's invalid.它说它是无效的。 The compiler is simply too kind to you.编译器对你太客气了。 In Clang this works, but it might not in other compilers.在 Clang 中这有效,但在其他编译器中可能无效。

At least include it to suppress the warning.至少包括它以抑制警告。 Unnecessary warnings are annoying.不必要的警告很烦人。 Any program should compile with warnings treated as errors (I always enable that).任何程序都应该在编译时将警告视为错误(我总是启用它)。

It appears that that's your compiler's magic.看来这就是您的编译器的魔力。 Not including the necessary headers may work on your compiler (which I suppose is by Microsoft), but it won't necessarily compile elsewhere(that includes future versions of the same compiler).不包括必要的头文件可能适用于您的编译器(我想它是由 Microsoft 提供的),但它不一定会在其他地方编译(包括同一编译器的未来版本)。 Write standard-conforming, portable code.编写符合标准的可移植代码。

stdlib.h is of the general purpose standard header which includes functions of Dynamic Memory allocation and other Standard Functions. stdlib.h 属于通用标准 header,其中包括动态 Memory 分配功能和其他标准功能。

For example if you want to display a message at the end of the execution of your program you will need to go for the getch() function,this functions reads a character from keyboard thus giving user the time to read the displayed Information.例如,如果您想在程序执行结束时显示一条消息,您需要 go 用于 getch() function,此函数从键盘读取一个字符,从而让用户有时间阅读显示的信息。

The getch() function requires the stdlib header to be Included. getch() function 需要包含标准库 header。

Like many things in c the reason that an error isn't generated when there is no prototype is for historical reasons.像 c 中的许多东西一样,没有原型时不会产生错误的原因是历史原因。 In the early days people often didn't bother prototyping functions because pointers and integers were usually the same size and integral types smaller than an integer were promoted to an integer when passed as a parameter (and floating point was rarely used for systems programming).在早期,人们通常不会打扰原型函数,因为指针和整数通常大小相同,并且小于 integer 的整数类型在作为参数传递时被提升为 integer(并且浮点很少用于系统编程)。

If at any point they had changed the compiler to give an error if a function was not prototyped then it would have broken many programs and would not have gained widespread acceptance.如果在 function 没有原型化的情况下,他们在任何时候更改了编译器以给出错误,那么它将破坏许多程序并且不会获得广泛接受。

With 64 bit addressing we are now entering a period when integers and pointers are not the same size and programs will most likely break if you do not prototype functions like malloc() that return a pointer.使用 64 位寻址,我们现在进入了整数和指针大小不同的时期,如果您没有像 malloc() 这样返回指针的函数原型,程序很可能会中断。

In gcc always set the following options for your own programs: -Werror -Wstrict-prototypes在 gcc 中,始终为您自己的程序设置以下选项: -Werror -Wstrict-prototypes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM