Any fundamental difference between source and header files in C?

前端 未结 4 1094
故里飘歌
故里飘歌 2020-12-02 07:40

I don\'t quite understand how things should be separated in C\'s source and header files. I often see many projects with two sets of files with the same name (sans the exten

4条回答
  •  不知归路
    2020-12-02 08:41

    There is little fundamental difference between .c and .h files (though some compilers may refuse to compile a raw .h file). The difference is more by convention.

    Typically the .h file provides the API and the .c provides the implementation.

    Therefore the .h file would contain only things needed by other source files to access the facilities provided by your .c file. So the .h files would provide the function prototypes of global functions, declarations of global variables (if you really must have them), and the structures and other types used by them. (Don't expose a structure if the only a pointer to the structure is required by the API.)

    In-line functions are also often included in .h files but some coding guidelines prefer the use of a separate extension (e.g. .inl)

    All other function implementations, the definition and initialisation of variables and declarations of local (static) variables and functions would be in the .c file.

提交回复
热议问题