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 ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20

    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"

    please explain me clearly and thx in advance.

  2. #2
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,439
    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.

    Quote Originally Posted by time4f5 View Post
    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.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    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.
    Last edited by time4f5; 07-11-2011 at 03:00 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,439
    Quote Originally Posted by time4f5 View Post
    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.
    Code:
    #include <stdio.h>    /*  as we are using printf()  */
    
    /*   Note we do not include <math.h>  */
    
    int main()
    {
         int i;
         double x = 1.0;
    
         for (i = 0; i < 4; ++i)
         {
                 x *= 2.0;    /*   a basic floating point operation */
                printf("%f ", x);
         }
         printf("\n");
         return 0;
    }
    Linking that program may be another story (as it may be necessary to link in floating point support, in order to print out floating point values) but the source code is valid.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Location
    India
    Posts
    20
    many thx for the explanation...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 08-09-2009, 11:20 AM
  2. Question about the book "The C Programming Language"
    By caduardo21 in forum C Programming
    Replies: 4
    Last Post: 05-15-2005, 01:22 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM
  5. "The C++ Programming Language"
    By d00b in forum C++ Programming
    Replies: 6
    Last Post: 06-14-2002, 12:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21