Doubt in "C Programming Language by K & R"

This is a discussion on Doubt in "C Programming Language by K & R" within the C Programming forums, part of the General Programming Boards category; I couldn't able to understand "The usual practice is to collect extern declarations of variables and functions in a separate ...

Doubt in "C Programming Language by K & R"

I couldn't able to understand "The usual practice is to collect extern declarations of variables and functions in a separate file, historically called a header, that is included by #include at the front of each source file. The suffix .h is conventional for header names"

in C the .h is a header file that is includes a library. a library is a 'set of instructions' for the program to run. This way you don't have to tell the program everything. It knows what an integer is because it is in the stdio.h library defines int and so on. it is kind of the brains of the program.

A header file, by convention, generally contains declarations that may be needed in more than one source file. Extern variable declarations are (as may be inferred from my description) one type of declaration. By placing declarations in the header file, if they are needed by multiple source files, then each source file than needs the particular set of declarations can #include the appropriate header.

This is generally considered preferable to manually copying the declarations into each source file, as that is error prone. It is error prone because an error may be made copying or, if the declarations are updated in one source file, the programmer must remember to correctly update EVERY source file with the same declarations. If one declaration is copied or updated incorrectly, various problems occur. Examples of such problems are multiple definitions or missing definitions reported at link time (both problems are fatal errors for the linker, which prevent an executable being produced).

By convention, header files often have a .h extension. That is not mandated by the C standard, except for standard headers.

Originally Posted by time4f5

in C the .h is a header file that is includes a library. a library is a 'set of instructions' for the program to run. This way you don't have to tell the program everything. It knows what an integer is because it is in the stdio.h library defines int and so on. it is kind of the brains of the program.

I'm sorry, but that's rubbish.

Header files do not generally include libraries. They contain declarations (for example, extern variable declarations or function declarations) that give information to the compiler about the interface supported by an external library. In some special cases, header files may define inline functions, but that is done as C source code, not some special "set of instructions".

Basic types (int, char, float, etc) are not defined in any header file. Support for basic types is implemented by the compiler, not in any header file (standard or otherwise). Some data characterising basic types are provided in standard headers (for example, a macro named INT_MAX within the standard header <limits.h> expands to the maximum value of an int), but the types themselves are not defined in header files.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

I guess I am wrong. thanks for the heads up. I always assumed that I had to include certain libraries math.h if I wanted to do math functions and not give all of the instructions.

<math.h> declares a number of mathematical functions (for example, pow(), exp(), etc). If you are not using those functions (or other things declared in <math.h>) then you do not need to #include that header.

Basic mathematical operations (for example, multiplication, division, etc) on floating point variables do not require inclusion of <math.h>. So the following is quite valid.

If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.