August 18th, 2012, 03:08 AM
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?
August 18th, 2012, 05:02 AM
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
August 18th, 2012, 06:52 AM
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:
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
August 19th, 2012, 03:10 AM
There are multiple C libraries?!! Isn't there a SINGLE, official C library?
August 19th, 2012, 04:38 AM
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.
Originally Posted by st_crab
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.
September 3rd, 2012, 10:18 AM
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.
// 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
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?!
September 3rd, 2012, 10:31 AM
I ♥ ManiacDan & requinix
This is a sig, and not necessarily
a comment on the OP:
don't be a help vampire
September 3rd, 2012, 10:48 AM
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.
Originally Posted by st_crab
Imagine, for instance, the source file
/* ... */
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".
September 3rd, 2012, 11:10 AM
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?
September 3rd, 2012, 01:42 PM
The C11 Standard (free draft (PDF)) states in section 7.1.2/4
So, GNU C is just being Standard compliant :-)
September 4th, 2012, 04:52 AM
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?
September 4th, 2012, 06:06 AM
The Standard makes reference to the (less than 30) standard headers only.
User defined headers need not follow the same rules.
September 4th, 2012, 08:14 AM
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.
September 4th, 2012, 08:16 AM
I am intrigued - what parts of the standard C library must be written in assembler?
September 4th, 2012, 09:14 AM
Hmmm ... that may have been an overstatement.
Originally Posted by clifford
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"