Search code examples
cheader-filesfunction-prototypes

The reason why we need a function prototype for methods that is imported from its header file


I am currently studying about pointer from K&R.

In page, 109 the authors declare function prototypes for methods

int getline(char *, int);
char *alloc(int);

even after, the source file import their standard libraries in the beginning.

#include<stdio.h>
#include<string.h>

isn't their declaration is done in their header file? What is the reason the authors declare the function prototypes of these methods when they are already declared in their own header files?


Solution

  • There is a getline function introduced around 2010 (long after the book was written) but it is not the same as the function you have mentioned. The library function's prototype is:

    size_t getline (char ** __lineptr, size_t * __n, FILE * __stream) ;
    

    Also there is no alloc in C. There are three similarly named library functions for memory allocation.

    void *malloc(size_t n);
    void *calloc(size_t n, size_t size
    void *realloc(void *p, size_t size) 
    

    I believe you will find the above functions as part of library in K&R.

    So K&R had to declare the prototypes of getline and alloc functions and implement them.