Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we include stdlib.h?

Tags:

c

C function malloc() is defined under 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? 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]); 
}
like image 834
schrodinger Avatar asked Nov 29 '22 10:11

schrodinger


1 Answers

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.

like image 197
Karoly Horvath Avatar answered Dec 21 '22 06:12

Karoly Horvath