Function header files

Discussion in 'C Programming' started by alanxx, Feb 16, 2011.

  1. alanxx

    alanxx Guest

    Hi,

    I am a student of C/C++. One of the mistakes I often make is forgetting to include the appropriate header files for the standard functions I use. Is there an easy way (a website?) to find out where a certain standard function is prototyped in? I can find answers via google search or MSDN library, but it's more time consuming than I'd prefer. Ideally, I would prefer to just go to a website, type in the name of the function in a search box and find out what header file I should include and how it is defined. Thanks for your help!

    alanxx
     
    alanxx, Feb 16, 2011
    #1
    1. Advertising

  2. alanxx

    Tom St Denis Guest

    On Feb 15, 7:04 pm, alanxx <> wrote:
    > Hi,
    >
    > I am a student of C/C++. One of the mistakes I often make is forgetting to include the appropriate header files for the standard functions I use. Is there an easy way (a website?) to find out where a certain standard function is prototyped in? I can find answers via google search or MSDN library, but it's more time consuming than I'd prefer. Ideally, I would prefer to just go to a website, type in the name of the function in a search box and find out what header file I should include and how it is defined. Thanks for your help!


    If you were using a proper *NIX distro I'd say just read the man
    pages.

    Google is your friend otherwise.

    Tom
     
    Tom St Denis, Feb 16, 2011
    #2
    1. Advertising

  3. alanxx

    Ian Collins Guest

    On 02/16/11 01:04 PM, alanxx wrote:
    > Hi,
    >
    > I am a student of C/C++. One of the mistakes I often make is forgetting to include the appropriate header files for the standard
    > functions I use. Is there an easy way (a website?) to find out where a certain standard function is prototyped in?


    Well there aren't that many to remember!

    On a Unix or Unix like system, just type man function, for others you
    could use the search box on the online Unix specification:

    http://www.opengroup.org/onlinepubs/000095399/

    --
    Ian Collins
     
    Ian Collins, Feb 16, 2011
    #3
  4. On Feb 15, 6:04 pm, alanxx <> wrote:
    > Hi,
    >
    > I am a student of C/C++. One of the mistakes I often make is forgetting to include the appropriate header files for the standard functions I use. Is there an easy way (a website?) to find out where a certain standard function is prototyped in? I can find answers via google search or MSDN library, but it's more time consuming than I'd prefer. Ideally, I would prefer to just go to a website, type in the name of the function in a search box and find out what header file I should include and how it is defined. Thanks for your help!
    >
    > alanxx


    Assuming the header files really are files (the standard does not
    require this), you could do a filesystem search.

    on unix:
    grep -d recurse funcname /usr/include

    on windows there's some kind of FindFile wizard where you can
    search for files 'Containing text:'.

    But you may get many spurious results.
     
    luser- -droog, Feb 16, 2011
    #4
  5. alanxx <> writes:
    > I am a student of C/C++.


    Oh, I hope not!

    If you mean that you're a student of C and of C++, that's fine.
    The problem is that "C/C++" is often used to refer to a mythical
    language that's some combination of the two. C and C++ are closely
    related by quite distinct languages.

    > One of the mistakes I often make
    > is forgetting to include the appropriate header files for the
    > standard functions I use. Is there an easy way (a website?) to
    > find out where a certain standard function is prototyped in? I
    > can find answers via google search or MSDN library, but it's more
    > time consuming than I'd prefer. Ideally, I would prefer to just
    > go to a website, type in the name of the function in a search
    > box and find out what header file I should include and how it is
    > defined. Thanks for your help!


    Whatever documentation you're using that tells you how to call the
    function *should* tell you which header you need to include.

    Does your IDE (if you're using one) provide documentation for standard
    functions?

    On Unix-like systems, the man page for each function tells you what you
    need to include. Try <http://www.linuxmanpages.com/>. (This won't be
    helpful for MS-specific or Windows-specific functions.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 16, 2011
    #5
  6. luser- -droog <> writes:
    > On Feb 15, 6:04 pm, alanxx <> wrote:
    >> I am a student of C/C++. One of the mistakes I often make is
    >> forgetting to include the appropriate header files for the
    >> standard functions I use. Is there an easy way (a website?) to
    >> find out where a certain standard function is prototyped in? I
    >> can find answers via google search or MSDN library, but it's
    >> more time consuming than I'd prefer. Ideally, I would prefer
    >> to just go to a website, type in the name of the function in a
    >> search box and find out what header file I should include and
    >> how it is defined. Thanks for your help!

    >
    > Assuming the header files really are files (the standard does not
    > require this), you could do a filesystem search.
    >
    > on unix:
    > grep -d recurse funcname /usr/include
    >
    > on windows there's some kind of FindFile wizard where you can
    > search for files 'Containing text:'.
    >
    > But you may get many spurious results.


    You almost certainly *will* get spurious results -- lots of them.

    On my system, for example (Ubuntu 10.04), 28 files under /usr/include
    that contain the word "fprintf". And the compiler searches 3
    directories that aren't even under /usr/include.

    Read the documentation. System header files are intended to be
    read by the compiler, not by the programmer. (That's not to say
    you shouldn't read them; you can learn a lot that way. But keep
    in mind that the code in system headers needn't be portable, or
    even necessarily legal C.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 16, 2011
    #6
  7. pete <> writes:
    > alanxx wrote:
    >> Is there an easy way (a website?)
    >> to find out where a certain standard function is prototyped in?

    >
    > Here is a free draft of the next standard:
    >
    > http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1547.pdf
    >
    > It is easy to use the C standard
    > to find out where a certain standard function is prototyped in.

    [...]

    Nobody implements that version of the standard yet.

    If your goal is to look up standard functions supported by your
    compiler, use this draft of the *current* standard:

    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf

    (That's a draft of the C99 standard; most compilers don't even
    *fully* implement that yet.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 16, 2011
    #7
  8. On Feb 16, 2:04 am, alanxx <> wrote:
    > Hi,
    >
    > I am a student of C/C++. One of the mistakes I often make is forgetting to include the appropriate header files for the standard functions I use.
    >

    If it does input/output stdio.h. If it's a trivial function that works
    on a string or area of memory, string.h. If it's a mathematical
    function, math.h. If it's malloc() and family or anything else,
    stdlib.h. Major exceptions - ctype.h contains is is... macros.
    assert.h has its own header for some reason. So does time.h.

    It's a reasonably logical division, though with a few quirks.
     
    Malcolm McLean, Feb 16, 2011
    #8
  9. alanxx

    jacob navia Guest

    Le 16/02/11 01:04, alanxx a écrit :
    > Hi,
    >
    > I am a student of C/C++. One of the mistakes I often make is forgetting to

    include the appropriate header files for the standard functions
    I use. Is there an easy way (a website?) to find out where a certain
    standard function is prototyped in? I can find answers via google search or
    MSDN library, but it's more time consuming than I'd prefer. Ideally, I
    would
    prefer to just go to a website, type in the name of the function in a
    search
    box and find out what header file I should include and how it is defined.

    Thanks for your help!
    >
    > alanxx



    Hi

    I am the author of the lcc-win compiler system

    If you use a decent IDE (under windows) put the cursor under the
    identifier you want to find out and press F1. At least that is what
    the IDE of lcc-win does. F1 calls the documentation and the
    docs tell you which include file is to be used and the library you need
    to add to the link (if any)

    But the most portable solution is this. I have built a header called
    stdheaders.h containing the following text:

    #include <assert.h>
    #include <complex.h>
    #include <ctype.h>
    #include <fenv.h>
    #include <float.h>
    #include <inttypes.h>
    #include <limits.h>
    #include <locale.h>
    #include <math.h>
    #include <setjmp.h>
    #include <signal.h>
    #include <stdarg.h>
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    #include <wchar.h>
    #include <wctype.h>
    #include <conio.h>
    #include <malloc.h>
    #include <process.h>

    Now I do not ever forget any standard header. Since I use a very fast
    compiler the extra milliseconds used in parsing those headers have
    no importance
     
    jacob navia, Feb 16, 2011
    #9
  10. jacob navia wrote:
    > #include <conio.h>


    "conio.h is a C header file used in old MS-DOS compilers to create text
    user interfaces. It is not described in The C Programming Language book,
    and it is not part of the C standard library, ISO C nor is it required
    by POSIX."

    Sorry don't take seriously, just couldn't resist :)


    -rasp
     
    Ralph Spitzner, Feb 16, 2011
    #10
  11. On Feb 16, 2:00 pm, Ralph Spitzner <> wrote:
    > jacob navia wrote:
    > > #include <conio.h>

    >
    > "conio.h is a C header file used in old MS-DOS compilers to create text
    > user interfaces. It is not described in The C Programming Language book,
    > and it is not part of the C standard library, ISO C nor is it required
    > by POSIX."
    >
    > Sorry don't take seriously, just couldn't resist :)
    >
    >         -rasp
    >

    He's got malloc.h in there as well, which is another DOS-ism.
     
    Malcolm McLean, Feb 16, 2011
    #11
  12. Malcolm McLean <> writes:

    > On Feb 16, 2:04 am, alanxx <> wrote:
    >> I am a student of C/C++. One of the mistakes I often make is
    >> forgetting to include the appropriate header files for the standard
    >> functions I use.
    >>

    > If it does input/output stdio.h.


    There are a lot of IO function not declared there. wchar.h declares all
    the "wide" IO functions.

    > If it's a trivial function that works
    > on a string or area of memory, string.h.


    Except for those that operate on wide or multi-byte strings.

    > If it's a mathematical
    > function, math.h.


    Except for the type generic maths functions declared in tgmath.h and all
    the complex mathematical functions declared in complex.h.

    > If it's malloc() and family or anything else,
    > stdlib.h. Major exceptions - ctype.h contains [the] is... macros.


    And wctype.h declares the isw... functions. By the way, what you call
    the is... macros must be functions, though they can be macros as well.

    > assert.h has its own header for some reason. So does time.h.


    And there is also stdint.h, inttypes.h, bool.h, complex.h, float.h,
    errno.h, setjmp.h, signal.h, stdarg.h, locale.h and fenv.h (and that's
    not all of them).

    > It's a reasonably logical division, though with a few quirks.


    Unfortunately the logic requires some knowledge of the history. For
    example the wide IO and string functions are separate because they are
    new.

    My point is not to enumerate the errors in your classification but to
    show that your general point -- which seems to be that it's not too hard
    to remember -- is not really true anymore.

    --
    Ben.
     
    Ben Bacarisse, Feb 16, 2011
    #12
  13. alanxx

    ImpalerCore Guest

    On Feb 15, 7:04 pm, alanxx <> wrote:
    > Hi,
    >
    > I am a student of C/C++. One of the mistakes I often make is forgetting to include the appropriate header files for the standard functions I use. Is there an easy way (a website?) to find out where a certain standard function is prototyped in? I can find answers via google search or MSDN library, but it's more time consuming than I'd prefer. Ideally, I would prefer to just go to a website, type in the name of the function in a search box and find out what header file I should include and how it is defined. Thanks for your help!
    >
    > alanxx


    The cplusplus.com website might be what you're looking for. If you
    type in a function name in the search box, you get a web page of the
    function plus its header on the navigation bar to the left.

    Hope it helps.
    John D.
     
    ImpalerCore, Feb 16, 2011
    #13
  14. alanxx

    jacob navia Guest

    Le 16/02/11 13:00, Ralph Spitzner a écrit :
    > jacob navia wrote:
    >> #include <conio.h>

    >
    > "conio.h is a C header file used in old MS-DOS compilers to create text
    > user interfaces. It is not described in The C Programming Language book,
    > and it is not part of the C standard library, ISO C nor is it required
    > by POSIX."
    >
    > Sorry don't take seriously, just couldn't resist :)
    >
    >
    > -rasp
    >
    >


    conio.h describes the interface for old Dos functions in lcc-win that
    allows you to use the old Borland "gotoxy()" text mode packages and
    have text mode with colors and simple "graphics" using special
    characters.

    If you do not like it you just erase that line. The concept is clear.
     
    jacob navia, Feb 16, 2011
    #14
  15. jacob navia wrote:
    > Le 16/02/11 13:00, Ralph Spitzner a écrit :
    >>
    >> Sorry don't take seriously, just couldn't resist :)

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    > If you do not like it you just erase that line. The concept is clear.
    >



    Not flaming you here. I just thought of it as being 'funny' finding such
    a dinosaur, looking at all the C99 etc. discussion going on here.

    -rasp
     
    Ralph Spitzner, Feb 16, 2011
    #15
  16. alanxx

    Tom St Denis Guest

    On Feb 16, 9:47 am, jacob navia <> wrote:
    > Le 16/02/11 13:00, Ralph Spitzner a écrit :
    >
    > > jacob navia wrote:
    > >> #include <conio.h>

    >
    > > "conio.h is a C header file used in old MS-DOS compilers to create text
    > > user interfaces. It is not described in The C Programming Language book,
    > > and it is not part of the C standard library, ISO C nor is it required
    > > by POSIX."

    >
    > > Sorry don't take seriously, just couldn't resist :)

    >
    > > -rasp

    >
    > conio.h describes the interface for old Dos functions in lcc-win that
    > allows you to use the old Borland "gotoxy()" text mode packages and
    > have text mode with colors and simple "graphics" using special
    > characters.
    >
    > If you do not like it you just erase that line. The concept is clear.


    Along the lines you're talking about it's not uncommon to write a
    "project.h" file which includes the relevant header files for the
    project. I wouldn't just blindly include all of the headers from /usr/
    include mostly because they're not all compatible but also it DOES add
    to compile times (hint: think of builds over NFS).

    Tom
     
    Tom St Denis, Feb 16, 2011
    #16
  17. jacob navia <> writes:
    > Le 16/02/11 13:00, Ralph Spitzner a ecrit :
    >> jacob navia wrote:
    >>> #include <conio.h>

    >> "conio.h is a C header file used in old MS-DOS compilers to create text
    >> user interfaces. It is not described in The C Programming Language book,
    >> and it is not part of the C standard library, ISO C nor is it required
    >> by POSIX."
    >>
    >> Sorry don't take seriously, just couldn't resist :)

    >
    > conio.h describes the interface for old Dos functions in lcc-win that
    > allows you to use the old Borland "gotoxy()" text mode packages and
    > have text mode with colors and simple "graphics" using special
    > characters.
    >
    > If you do not like it you just erase that line. The concept is clear.


    Fair enough. I suppose there's a similar story for <process.h>.

    But why is <malloc.h> in the list? Does it give you something that
    <stdlib.h> doesn't?

    If you're going to use that approach, perhaps it would be better to have
    "stdheader.h" include just the standard headers; if you're using
    features from <conio.h> it might be better to emphasize that by
    including it directly in your source file.

    (Personally I've never done that kind of thing, and it *feels* like
    poor style, but I'm not sure I can articulate why.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 16, 2011
    #17
  18. On Feb 16, 11:00 am, Keith Thompson <> wrote:
    > (Personally I've never done that kind of thing, and it *feels* like
    > poor style, but I'm not sure I can articulate why.)


    I'm not sure it is bad "style", but it can certainly cause performance
    issues in a large scale build to access/read/parse all the unneeded
    header files, particularly if they aren't on a local filesystem.
    Certainly, including unneeded headers that might change (standard
    headers
    don't much in practice) introduces unneeded dependencies into the
    build process.
    It is vexing if someone touches a file that shouldn't trigger a
    global
    rebuild of targets but does.

    To OP, I'd personally just learn which standard functions go with
    which headers.
    May take a while, but the C standard library isn't THAT huge...

    -David
     
    David Resnick, Feb 16, 2011
    #18
  19. ImpalerCore <> writes:

    > On Feb 15, 7:04 pm, alanxx <> wrote:
    >> Hi,
    >>
    >> I am a student of C/C++. One of the mistakes I often make is
    >> forgetting to include the appropriate header files for the standard
    >> functions I use. Is there an easy way (a website?) to find out where
    >> a certain standard function is prototyped in? I can find answers via
    >> google search or MSDN library, but it's more time consuming than I'd
    >> prefer. Ideally, I would prefer to just go to a website, type in the
    >> name of the function in a search box and find out what header file I
    >> should include and how it is defined. Thanks for your help!
    >>
    >> alanxx

    >
    > The cplusplus.com website might be what you're looking for. If you
    > type in a function name in the search box, you get a web page of the
    > function plus its header on the navigation bar to the left.


    I'd advise against using this site unless you are working in C++.
    It works for common C functions but fails for a lot of newer C99
    functions.

    --
    Ben.
     
    Ben Bacarisse, Feb 16, 2011
    #19
  20. David Resnick <> writes:
    > On Feb 16, 11:00 am, Keith Thompson <> wrote:
    >> (Personally I've never done that kind of thing, and it *feels* like
    >> poor style, but I'm not sure I can articulate why.)

    >
    > I'm not sure it is bad "style", but it can certainly cause performance
    > issues in a large scale build to access/read/parse all the unneeded
    > header files, particularly if they aren't on a local filesystem.
    > Certainly, including unneeded headers that might change (standard
    > headers don't much in practice) introduces unneeded dependencies into
    > the build process. It is vexing if someone touches a file that
    > shouldn't trigger a global rebuild of targets but does.


    For the truly standard headers (those defined by the language
    standard), updates shouldn't be much of an issue -- and if the
    standard headers provided by the implementation are updated, you
    probably *should* recompile everything.

    I suppose there's a corner case where you have a "stdheader.h",
    and the implementation is updated piecemeal, so an update that
    affects only <math.h> causes an unnecessary rebuild. I don't know
    how often that would occur in practice.

    > To OP, I'd personally just learn which standard functions go with
    > which headers. May take a while, but the C standard library isn't
    > THAT huge...


    I tend to agree.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 16, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    4
    Views:
    750
    lilburne
    Nov 2, 2003
  2. John Smith

    Header files included in header files

    John Smith, Jul 21, 2004, in forum: C Programming
    Replies:
    18
    Views:
    639
    Jack Klein
    Jul 24, 2004
  3. Replies:
    3
    Views:
    1,360
    Walter Roberson
    May 1, 2006
  4. Umesh

    Function & header files

    Umesh, Jun 12, 2007, in forum: C Programming
    Replies:
    34
    Views:
    945
    Dave Vandervies
    Jun 16, 2007
  5. mlt
    Replies:
    2
    Views:
    916
    Jean-Marc Bourguet
    Jan 31, 2009
Loading...

Share This Page