34

I wanted to have a look at the implementation of different C/C++ functions (like strcpy, stcmp, strstr). This will help me in knowing good coding practices in c/c++. Could you let me know where can I find it?

Thanks.

  • Is it possible to get the source code of the standard c library? –  Jul 14 '09 at 19:00
  • Each C compiler will use its own source code for the standard C library. Similarly, each C++ compiler will use its own source code for the standard C and C++ libraries (C++ includes C so a compiler needs both libraries). However, it's also possible that strcpy() is directly implemented by the compiler; it could simply emit the correct code in place instead of creating a function call. – MSalters Jul 15 '09 at 12:04
  • So to get a particular library implementation, one must find out whether the people who wrote the compiler of interest made the source code available? – Minh Tran Oct 24 '16 at 02:45

14 Answers14

32

You could check out a copy of glibc, which will have the source code for all C functions in the C standard library. That should be a good starting place.

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 3
    Although this is the first answer that comes to mind, it may not be the best place to learn about "good coding practices" that have general applicability. This is because the glibc functions have a legitmate reason to be heavily micro-optimised - even platform-specific optimisations - simply because they are standard library functions. That's not normally a good practice for general programming. – caf Jul 15 '09 at 07:47
  • @mipadi I clicked the link and went inside sources and checked http://ftp.gnu.org/gnu/glibc/ . There is no math.h in any of .tar.gz files. I just want to look at sqrt(). Not to write a optimized function. Just to look at how C does it – Jsmith Jan 07 '17 at 13:08
18

It's worth pointing out that the source code for the C standard library does not necessarily highlight good coding practices. As the standard library, it has a special status where, for example, it can rely on a lot of unspecified or non-portable behavior simply because they know the exact compiler with which it is used.

And of course, this only tells you about C coding practices.

That has nothing to do with C++ coding practices, which are very different. Don't treat them as one language. There is no such thing as C/C++. C is a language with its own coding practices and common idioms, and C++ is a separate, independent language which also has its own practices and idioms.

Good C code is rarely good C++ code. strcpy and other C library functions are certainly not good C++ code.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • This was my first thought when I saw the question. It is arguable that the best C libaries ARE processor specific. A general C lib written in just C and minimal amounts of processor/architecture specific functionality is going to have crappy performance compared to a targeted libary for the particular system. This even more of a concern on handheld platforms where the general excuses for inefficient code don't apply. (i.e. It's not going to be ran on a 3+GHz octo-core processor with infinite memory.) – NoMoreZealots Jul 14 '09 at 19:48
  • Sorry if that sounded preachy, but I get annoyed everytime I boot my PC and it takes longer than a system I had 20 years ago. People take the "little efficiencies" argument to a level of insanity on modern systems. The whole "netbook" and handheld market makes me giddy when I hear people whine about "it's not powerful enough" for there applications. Now you actually have to THINK, ain't that a bitch! – NoMoreZealots Jul 14 '09 at 19:54
  • I completely agree, but coming from the other direction. I write scientific applications made to run on clusters with 1000+ cores. At this scale, those little inefficiencies really add up. In 18 months when we get a new system with twice the problem, our problems have at least doubled in size. The hardware is never going to catch up to what we want to do. – KeithB Jul 14 '09 at 21:29
  • I agree that you should never write inefficient code, speaking of the actual algorithmic constructs themselves. What I don't agree with is people who are always bashing managed code for being slow. I personally think that writing clean, small, organized, and mostly 'portable' code is more important in most cases than efficiency. The hardware will get faster in the future and good code will be reused; crappy, messy, non-portable code will just become obsolete, and we'll have to reinvent that wheel *again*. – LoveMeSomeCode Jul 14 '09 at 21:38
  • 2
    I don't think anyone even mentioned managed code. In most cases, there is also no contradiction between clean/portable and efficient code. And people thinking that *only* the algorithmic efficiency matters are just missing the point. – jalf Jul 14 '09 at 22:03
  • the algorithmic efficiency, on different levels, is the whole of efficiency. I'm speaking in generic terms where the algorithm is your whole plan of how to do something. And I only brought up managed code because someone always does in an efficiency debate, and I'm very pro-managed-code. Yeah, I know C can be faster than C#, but straight assembler can be faster than C, so why don't we do that all day? Oh that's right, because hardware is cheaper than software people. – LoveMeSomeCode Jul 14 '09 at 22:13
  • Is it? Tell that to KeithB who's working with huge cluster computers. Anyway, perhaps you should write a blog post about it. But since you really insist on the comparison, I should probably mention that there's no technical reason why C should be slower than ASM. Given a sufficiently good compiler, and well-written C code, it won't be. But managed code *always* has a cost, if nothing else then in startup time. Anyway, managed code is hardly the issue here. Nor is performance, come to think of it. Don't you people have blogs or something to post your pet peeves on? ;) – jalf Jul 14 '09 at 23:21
  • Actually managed code is a interesting example of why library MUST be efficient. If the managed code was running on non-optimized (down to the metal) libraries, it would run like horse crap. Nicklus Wirth had quote that went something like, the rate at which programs are becoming inefficient is greater than the rate computers are increasing in speed. – NoMoreZealots Jul 15 '09 at 21:37
  • @jalf This is old, but I have to say something with regards to your comment that "people thinking that only the algorithmic efficiency matters are just missing the point". Algorithmic efficiency just happens to generally be MUCH more important to get right. Implementing an O(n^2) algorithm to solve a problem that has O(logn) solutions will always be slower. You can micro-optimize all you want, but it won't make your n^2 algorithm faster. Basically - algorithmic efficiency should come first, then basic optimizations, then finally if need be, micro optimization. Ppl often get those backwards. – wallacer Jun 18 '15 at 17:12
8

Most compilers provide source code for the library - however that source code is usually rather more complex and convoluted than you might expect.

A good resource for this is P.J. Plauger's book, "The Standard C Library", which can be had pretty cheaply if you go for a used one. The code presented is not always straight-forward, but Plauger explains it quite well (and gives the reasons why it can't always be straight-forward and still follow the standard).

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Wow - it looks like used copies are no longer cheap. About $45 being the low end. I wonder why? – Michael Burr May 11 '16 at 05:37
  • Thanks to you, my friend :) – xxks-kkk May 11 '16 at 05:38
  • The explanations in the book are really worthwhile for understanding the library. It was, however, written in the early 90s, so it is for the C90 standard rather than C99 (let alone C11, which was but a work item in the standard committee's queue when this answer was written). Nevertheless, it is far more easily comprehensible code than the GNU C library code is. – Jonathan Leffler Mar 07 '18 at 04:31
6

Many C standard library functions have source code listings & discussions in The C Programming Language by Kernighan & Ritchie. The discussions are a helpful way of learning more about the specifics of the C language & how functions in the standard library work under the hood.

Pete
  • 10,310
  • 7
  • 53
  • 59
  • 2
    Many functions in K&R are shown in a simplified form but this may be better if they are being used for examples. – Nick Van Brunt Jul 14 '09 at 19:13
  • 2
    K&R should be required reading for anyone programming in C. Very few have written something so good in so few pages. – NoMoreZealots Jul 14 '09 at 19:59
  • 1
    This is a better suggestion than looking at real libraries. These were written with the goal of teaching C, rather than the goal of having the most efficient C library. – caf Jul 15 '09 at 07:51
5

Look at an implementation of the libc standard C library. To see how a real, popular C library is implemented, try looking at the glibc code. You can access the code using git:

git clone git://sourceware.org/git/glibc.git

As for C++, you can find the glibc++ standard library on one of these mirrors:

http://gcc.gnu.org/mirrors.html

You can also check out uLibc, which will probably be simpler than the GNU library:

http://git.uclibc.org/uClibc/tree/

To give you a flavour, here's the strncpy implementation from uLibc:

Wchar *Wstrcpy(Wchar * __restrict s1, const Wchar * __restrict s2)
{
    register Wchar *s = s1;

#ifdef __BCC__
    do {
        *s = *s2++;
    } while (*s++ != 0);
#else
    while ( (*s++ = *s2++) != 0 );
#endif

    return s1;
}

Here is strcmp and strstr

IRBMe
  • 4,335
  • 1
  • 21
  • 22
  • 6
    A great example of why real C library implementations aren't a good guide to generally-applicable good programming practices - having those two slightly-refactored versions of the same loop for different C compilers would almost never be justified in most of the programming you'll do. – caf Jul 15 '09 at 07:53
2

"The Standarc C Library" is a specification. You want the sources for an implementation of the specification. Your C compiler may or not provide such sources - one that does is GCC.

2

You could also have a look at the OpenBSD source tree. Specifically, you want the string subdirectory of libc.

2

About 10 years ago, I read The Standard C Library by Plauger. Thoroughly recommend it.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
1

standard c library

Tim Hoolihan
  • 12,316
  • 3
  • 41
  • 54
1

Here you go

The implementation will vary somewhat from OS to OS, but the GNU/Linux implementation is probably going to be the easiest one to find out there.

Eric Petroelje
  • 59,820
  • 9
  • 127
  • 177
0

Here's the general strXXX C functions in NetBSD : http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/string/

Here's the NetBSD strXXX implementation for i386 processors http://cvsweb.netbsd.org/bsdweb.cgi/src/common/lib/libc/arch/i386/string/

nos
  • 223,662
  • 58
  • 417
  • 506
0

If you use Visual Studio Professional or Team, you should be able to find the source here:

C:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
0

For your purposes, this book may be helpful: Mastering Algorithms with C

Frank V
  • 25,141
  • 34
  • 106
  • 144
0

In general, I find the BSD libc much easier to read than the GNU one. There are less "gcc-isms", the core is much clearer in intent, etc... For example, the BSD code for malloc is quite readable compared to glibc

David Cournapeau
  • 78,318
  • 8
  • 63
  • 70