Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Standard Library function definitions


    Hello! I'm new here. I just learnt C and I'm trying to explore it more. I wanted to peel down the layers and see what the standard library consists of.
    Now, I opened stdio.h (I'm using DevC++. I browse to the "include" directory to explore C's header files), to think that I'd find the full definitions of all the standard in/out functions, but all I find are just declarations! Where do the full definitions lie? How is a program, after including the stdio header file, able to call all those functions if their definitions aren't in the header file?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    The definitions of the functions in the Standard Library need not have been written in C (a few even cannot have been: they must be written in assembler).

    A library contains just the compiled code (optionally with debugging symbols and other info). The corresponding header file is there to help the compiler know how to interface the code in the library with the code it will generate when compiling your sources.

    Anyway ... a few libraries are open source and you can find the source code through Google.

    I believe the Microsoft libraries include source line information in their debugging versions. I can not help you with how to can link with the debugging versions of the libraries and see the information therein.

    Anyway, rather then reading through source code, I suggest you read a book about it.
    The Standard C Library, by PJ Plauger
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    C compilers come with a copy of the standard library. In some compilers, they actually give you the source code for the standar library, in others, they give it to you if you purchase the enterprise version of their product. In some cases, they don't give you the libc source at all.

    With that said, here's a libc that is used by OpenBSD and is freely available on the web:
    http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0
    There are multiple C libraries?!! Isn't there a SINGLE, official C library?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by st_crab
    There are multiple C libraries?!! Isn't there a SINGLE, official C library?
    hehe, there is a single (*) official single DESCRIPTION of the C library. The DESCRIPTION of the library states the name and workings of several functions, macros, and variables any binary library has to implement to be called a C library.

    All binary C libraries have to have all the functions described, and they must behave as described (they can have more though, and very often have).
    For example: a library where snprintf() returns a negative integer when there is not enough space, is not a C99 library.



    (*) There are, in fact, basically 3 such descriptions. The one published in 1989 by ANSI (same as the one published in 1990 by ISO), the one published in 1999 by ANSI and ISO, and the one published in 2011 by ANSI and ISO. These DESCRIPTIONS are only text.
    You cand find free versions (the draft documents) of the latest two online as PDF files: C99 and C11.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0

    Thumbs up


    I see, thank you. But I do still want to peel the layers down, so how can I go about finding, seeing & understanding the assembly code of (Dev-C++'s) standard library?

    Though the book is tempting given its rating, I've found several resources on the web where the standard library's functions have been explained. So, on the fence about it...

    Also, while I'm here I have another somewhat related question. Rather than clog the forums with another amateur topic, I'll just ask it here - I saw this code in a header file of a software's source.

    Code:
    #ifndef MACHINE_TYPE_H
    #define MACHINE_TYPE_H
    #include <stdio.h>
    #include <ncurses.h>
    // number 1: datatypes
     
    typedef	unsigned char 		tBool;
    typedef	unsigned char 		tUInt8;
    typedef unsigned short		tUInt16;
    typedef	unsigned int		tUInt32;
    typedef	unsigned long long	tUInt64;
     
    typedef	signed char 		tInt8;
    typedef signed short		tInt16;
    typedef	signed int		tInt32;
    typedef	signed long long	tInt64;
     
    typedef	FILE*			tFptr;
     
    // number 2: file operations
    tUInt64	getfilepos(tFptr f);
    tUInt64 getfilesize(tFptr f);
    void	setfilepos(tFptr f,tUInt64 pos);
     
    #define	MIN(a,b)	(((a)<(b))?(a):(b))
    #define	MAX(a,b)	(((a)>(b))?(a):(b))
    #define	RETOK	0
    #define	RETNOK	-1
    #endif


    Its all fine and dandy except the first two lines. (And I may have seen similar cases in the standard library I mentioned earlier) - if MACHINE_TYPE_H macro isn't defined, then define it. But... define it to be what??? There's no replacement text mentioned in the second line! What is this sorcery?!
  12. #7
  13. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,555
    Rep Power
    2338
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by st_crab
    ... I saw this code in a header file ...

    [code=c]#ifndef MACHINE_TYPE_H
    #define MACHINE_TYPE_H
    /* ... */
    #endif
    [/code]

    Its all fine and dandy except the first two lines. (And I may have seen similar cases in the standard library I mentioned earlier) - if MACHINE_TYPE_H macro isn't defined, then define it. But... define it to be what??? There's no replacement text mentioned in the second line! What is this sorcery?!
    Just define it, so that the next time the same header is included (while compiling the same translation unit) errors of "duplicate identifiers" are avoided.

    Imagine, for instance, the source file
    [code=c]#include "foobar.h"
    #include "foobar.h"
    /* ... */
    [/code]

    The first time the header file is included, it defines, among other things, the type tUInt8.
    If there was no control over including the same file several times, the 2nd inclusion would create a duplicate definition.

    Remember the pre-processor is a very dumb text replacer. It simply replaces the include line with the contents of each header.

    You may want to google "header guard" or "include guard" or "header inclusion guard".
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0
    Oh! Interesting. However:
    "The GNU C Library header files have been written in such a way that it doesn't matter if a header file is accidentally included more than once; including a header file a second time has no effect. Likewise, if your program needs to include multiple header files, the order in which they are included doesn't matter. " - gnu.org/software/libc/manual/html_mono/libc.html So, I take it the above quote only applies to GNU C's standard library's header files and not all C std libs headers in general?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    The C11 Standard (free draft (PDF)) states in section 7.1.2/4

    Standard headers may be included in any order; each may be included more than once in
    a given scope, with no effect different from being included only once, except that the
    effect of including <assert.h> depends on the definition of NDEBUG (see 7.2). ...
    So, GNU C is just being Standard compliant :-)
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    13
    Rep Power
    0
    Hmm.. if the C standard itself asks for there to be no problems with multiple inclusion of header files, then why did the problem have to be addressed in the first place with the #ifndef preprocessor command?

    Also, how does the preprocessor distinguish at all between references to header files and replacement texts? Over at the wikipedia article for include guard, GRANDFATHER_H is a header file. But how does the preprocessor know that it isn't actually replacement text for, say I don't know, the integer 60? How does it automatically know that its a reference to the header file grandfather.h? Because of the use of the underscore character?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    if the C standard itself asks for there to be no problems with multiple inclusion of header files, then why did the problem have to be addressed in the first place with the #ifndef preprocessor command?
    The Standard makes reference to the (less than 30) standard headers only.

    User defined headers need not follow the same rules.
  24. #13
  25. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    In your case Dev-C++ uses the MinGW/GCC compiler. It uses Microsoft's C runtime library which is contained in msvcrt.dll - a dynamic link library that is shipped with the OS. If you have a Visual Studio Professional licence (not the free Express Edition version), that included the library source code and you can step into it with the debugger if you wish.

    Otherwise if you are just generally interested in standard library implementations, then you might take a look an open source library such as GNU libc , uClibc, or Newlib for example.
  26. #14
  27. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by bdb
    The definitions of the functions in the Standard Library need not have been written in C (a few even cannot have been: they must be written in assembler).
    I am intrigued - what parts of the standard C library must be written in assembler?
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by clifford
    I am intrigued - what parts of the standard C library must be written in assembler?
    Hmmm ... that may have been an overstatement.

    Anyway, parts of the library that have to deal with the Host Operating System (for allocating memory or opening streams for instance) cannot be written in "pure standard conforming (freestanding) C"
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo