Thread: Why do we use the header file in C

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Roorkee
    Posts
    3

    Question Why do we use the header file in C

    Why do we use the header file in C.

    Code:
    #include<stdio.h>
    void main()
    {
    printf("Hello");
    }
    OR

    Code:
    void main()
    {
    printf("Hello");
    }
    both returns same output then why do we use #include<stdio.h>
    and if include file is optional then how compiler works
    Last edited by Salem; 09-09-2007 at 10:20 AM. Reason: Fixed positioning of code tags, as noted by dwks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Put only your code inside code tags, not your whole post.

    main() should return int. http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    Why do you need header files?

    We'll only consider the function prototypes in header files here. Without a function prototype for printf(), the compiler has no idea what parameters printf() is supposed to take; so if you pass it the wrong ones, it can't tell you. If you do have a prototype, it knows that printf() takes a char* string for its first argument and takes a variable number of arguments after that, so if you use
    Code:
    printf(12);
    the compiler can complain.

    Most compilers will warn you if you use a function that is not prototyped. Since yours apparently didn't, I would recommend turning up its warning level.

    But there are other things in header files as well. Preprocessor constants and macros, for example, cannot be used unless you include the header file. This is because a preprocessor define might look like this:
    Code:
    #define BUFSIZ 512
    The preprocessor will search through your source code for "BUFSIZ" and replace it with 512. Without the header file, this does not occur and the compiler doesn't know what to do with "BUFSIZ".

    There are other data types that, if defined in a header file, require that header file to be used. structures, enumerations, typedefs, and unions, for example. Without <stdio.h>, you couldn't use the typedef FILE to perform file I/O.

    In short, it's a good idea to just include any header files that you need.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Visual Studio automatically includes some standard header files for you if you forget. This is wrong and extremely annoying. If there's a setting to turn off this "feature" I'd love to know, because I haven't found it yet.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    Roorkee
    Posts
    3
    Thanks for the replies cpjust and dwks, thing is clear that macros and constant cant be use without header file but when we run programs then how does compiler come to know some functions like getch(); without putting the conio.h in header file, and same with printf and scanf because as per my knowledge these are predefined under stdio.h

    Help me i m new to this C language and my faculty is asking to me for a week. hope you wont mind.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The compiler doesn't really care what the types and parameters to a function is, I gave an example earlier that in standard C will take variable number of arguments. Similarly, you can have variable types of arguments to the same function - it is then up to your code to do this correctly. Of course, if you call "printf(12.6);", the compiler will happily compile that (if you have warnings enabled it will probably give you a warning that the type is incorrect), but the compiler itself doesn't KNOW what type printf takes.

    The linker stage will just look for a function called printf(), it doesn't know (or care) what this function does, what arguments it takes, or anything like that[1].

    When it comes to your code being executed, there will be a serious problem, because printf expects the address of a text-string, not a floating point number. It will take the floating point number and try to use it as the address of a text-string [2]. This will lead to something incorrect - either some garbage output, or the program will crash.

    [1] C++ on the other hand has something called "name mangling", which means that C++ functions are "decorated" with "extra stuff" to mark what parameters the function takes, so the linker will not find the right function if you use the wrong argument types.

    [2] That is, if the compiler passes floating point numbers in the same way as it does integers and addresses. If that isn't the case, then some other "rubbish" will be used as the address of the text-string, which is most likely equally unpredictable and bad.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM