Firmware, system, and sensors development
"Free C/C++ Compilers"

"High-Performance Version Control"

Click here to learn more

The SNIPPETS C Source Code Archives
Sunday, 17-Feb-2008, 22:22:03 GMT
Last updated: 26-Nov-2007, 10:14:30 GMT
102661 visitors since 14-Sep-2003

Archivist: Bob Stout
Return to the SNIPPETS archives project Download the C archive in ZIP format
Join the C programming mailing list
Browse the C code archives Support SNIPPETS
In Memoriam
Auke Reitsma
For the 15 some-odd years that the SNIPPETS C code archives were in existence prior to his death, Auke Reitsma contributed materially to it. Aside from his code contributions which you'll find throughout the archives, he also gave selflessly of his time in helping collate, test, and proofread the material that went into the archives. The archives as you see them today wouldn't exist without his constant aid and encouragement. The C programming community lost a valuable contributor when Auke passed away in April, 2003, after a prolonged battle with cancer - and I lost a good friend and collaborator. What's odd, now that he's gone is the realization that at any time I could've passed him on the street and never recognized him - such is the nature of online partnerships. For his part in helping to make SNIPPETS a reality and to help keep the project alive, this site is forever dedicated to his memory.


The SNIPPETS collection started out in the late 1980's as a personal project to collect useful snippets (hence the name) of C, and later C++, code which would be free for reuse in a personal or commercial environment. Over time, it has grown to more than 94,000 lines of code in over 700 separate files. The goal of SNIPPETS is to collect and disseminate the best C/C++ answers to "How do I...?" programming questions.

Although it hasn't been well maintained since 1997, this is changing as SNIPPETS becomes primarily a web-based document. The first requirement for code to be included in SNIPPETS is that it must be either public domain, freeware, or carry a free use license. For further discussion of what makes code a candidate for SNIPPETS, see the rationale. Also changing rapidly is the fact that, previously most SNIPPETS code was tested on PC's under DOS/Windows, whereas more and more is now tested under Linux.

Note that this web site could be significantly improved cosmetically by the use of frames, Javascript, or other techniques. It exists as it is in order to be as compatible as possible with every imaginable browser, including text-mode browsers such as LYNX. To facilitate the downloading of only specific files of interest, the navigation menus will open each SNIPPETS file in a separate browser window, where it may be studied or easily copied.

The SNIPPETS C mailing list conference:

The SNIPPETS Archives Project for C exists to promote C education. It does this with the publication of free code as well as an associated discussion conference. Implemented as a lightly moderated Internet mailing list, the conference promotes C education by three of the most effetive means possible:
  1. Bring togethers programmers of all experience levels so the beginners can learn from experienced experts.
  2. One of the best ways to truly learn anything is to have to try to teach it to someone else. Both beginners and the "gurus" alike can sharpen their skills by joining in explanations. Sometimes, the perspective from a relative newcomer can be invaluable in getting a point across to someone new.
  3. The best answer to almost any programming question is to post code! Since one of the conference rules (see below) is that all posted code must be either explicitly public domain or carry a free use license, the code each subscriber posts becomes candidate code for the archives.
As a lightly moderated conference, the C conference has some rules. You can read these at Subscribe at


One major change in this release is in the testing philosophy. In the past, all SNIPPETS code was tested with PC compilers from Microsoft, Borland, Watcom, and Symantec/Zortech, and where possible, also under one or more of SysV Unix, BSD Unix, or Linux, using appropriate vendor or FSF compilers. Beginning with this release, only two free compilers have been used for testing: These compilers work with Unix (all flavors), Linux, DOS, and Win32. Accordingly, I have discontinued support for OS/2, Amiga, Win16, and other less mainstream OS's. You will still find some OS/2-specific legacy code in SNIPPETS, but it hasn't been tested since 1997 and I can't vouch for its usability. I may be able to provide continued support for Mac, but until such time as I can get a dedicated Mac compiler (probably Metrowerks CodeWarrior), further Mac support has been suspended. In the meantime, I suggest that users of these and other unsupported OS's look around for a port of GCC for their platform.

What does this change mean to the user? The bad news is that it may mean more work if you're using a compiler which isn't compatible with these two (which in turn means your compiler isn't compatible with either the standards or with Microsoft). The good news is that, released from the requirement of much of the conditional code used to support multiple compilers, SNIPPETS code is once again easy to read. This is especially true in light of the change in the organization of the SNIPPETS collection(s) - see "What's new?" below.

Compiling SNIPPETS code:

SNIPPETS files fall into 4 categories:

  1. Macros and true code snippets. These will not compile to anything meaningful by themselves. Macros are designed to be included in file headers, while code snippets are incomplete pieces of code used to demonstrate a general principle. Most SNIPPETS macros appear near the beginning of collections and/or sections archive in .H files. True snippets are rare, but should be obvious when you run across one.
  2. Functions. Most of the SNIPPETS files are functions. Many SNIPPETS functions also have test code built-in which will allow them to be compiled to stand-alone versions by defining the macro TEST during the compilation process. Individual functions appear in the archive immediately following the macros. SNIPPETS also contains larger, multi-file function packages, noted in each specific section.
  3. Stand-alone utilities. Many utilities contain useful functions and demonstrate useful C programming techniques. Most of these have been moved to the Applications section.
  4. Educational files. Between the individual functions and the function packages comprising two or more files, there is a short section of text files answering frequently-asked questions or providing tutorials on various aspects of C programming.

What's new in SNIPPETS?

As already noted, the most notable change is that SNIPPETS is now a web-based archive rather than ftp-based. However, much more fundamental changes have occurred in its organization. The most obvious of these changes is that the collection has been split into several separate collections, each targeted at a specific audience. Beginning with this (November, 2007) release of the web site, the following collections are supported:

What does this mean for SNIPPETS users? Well first of all, it means that much of the confusing platform-specific conditional code is no longer there. Secondly, it means that, rather than the conditional code, the same function may appear in several specific collections. Finally, and perhaps most obviously, it means that it will be easier for users to find the things they're really interested in without having to wade through files which aren't really appropriate for them.

Also, by splitting the collection in this way, it leaves the door open for others to contribute collections specific to other languages and environments. Stay tuned for further developments.

The SNIPPETS rationale

Where do SNIPPETS come from?

I'm constantly on the lookout for new SNIPPETS material. The goal of SNIPPETS has been to collect useful pieces of code that would be free for reuse. This limits consideration to code that is either explicitly in the public domain or which carries a free use license with few or no restrictions which would prevent its use. Current SNIPPETS material has been collected from the C_Echo (a Fidonet Echomail group, the C_Echo has been gated to Internet mailing lists and may eventually be gated to the SNIPPETS C mailing list as well), the comp.lang.c newsgroup, various public domain archives, submissions, and my own commercial library (MFL, now out of print).

How are SNIPPETS selected?

As the collection has evolved, it's taken on distinguishing characteristics based largely, I'm afraid, on what sorts of things interest me. A primary focus of SNIPPETS is portability - first of all between DOS compilers, and secondly with the larger world of Unix and other platforms. I tend to be interested in the same problem domain as Posix - system and utility functions. What I previously have not been interested in trying to include are things which really require massive amounts of code to do right (e.g. GUIs, databases, interrupt-driven comm I/O, game-quality graphics, multimedia, etc.) and are therefore better addressed by large single-purpose packages, whether commercial or otherwise. However, due to the other changes in SNIPPETS, these will henceforth be considered as well.

Since I also make my living programming embedded systems, you'll also find a number of functions useful in an embedded environment. This is also the reason why there's still so much DOS code in SNIPPETS - DOS is still quite commonly used on embedded single board computers (SBC's).

In addition to the utility stuff, I also snag anything that seems moderately obscure (e.g. ISBN validation, wind chill, sunrise/set time, etc.) Whether I need to know these things or not, someone sometime will need to be able to do these and I like to be able to provide the option of a simple canned solution rather than sending them on a research spree.

What makes one SNIPPETS entry "better" than other candidates?

Anything that enhances portability or usability is better in my book. Note that I'm not necessarily soliciting completion to full library standards. As the name SNIPPETS implies, many of the entries knowingly have minimal bullet-proofing. Most of the entries I've excerpted from MFL differ only in that my commercial code provides bullet-proof error trapping at the cost of slightly obfuscating the algorithms. In cases where error trapping would, in my opinion, make the algorithm harder to follow, I've generally opted for clarity over safety. This was a conscious decision since the target audience for SNIPPETS includes both newbies and experienced programmers. The pros will recognize the shortcomings and know to add the error trapping, but the tyros can tend to get lost. My yardstick on this has been feedback. If too many tyros have to ask how it works, then I try to simplify and/or make the underlying algorithm stand out. If too many folks complain about it being unsafe, I add more bullet-proofing. When I hear as little from both sides as possible, I figure I've achieved the desired balance.

Is the SNIPPETS collection only for PC's?

No. More and more portable code has been introduced into the collection over the years so that now there is more portable code (although some will require minor work) than PC-specific code.

Is SNIPPETS only a collection of C code?

No. There have always been a few PC-specific assembly routines in SNIPPETS, and the current collection includes a growing number of reusable C++ classes. In some cases, SNIPPETS includes both a C and C++ solution to a particular programming question. However, as the SNIPPETS archives project broadens, many of these files will be moved to the new areas.

Legal/copyright information

All the code I put into SNIPPETS for distribution is Public Domain or free to the best that I can determine. What this means is that:

  1. I know or can contact the original author(s) to verify the presumed copyright ownership, and
  2. The work bears an explicit Public Domain notice, or
  3. The work is copyrighted but includes a free use license, or
  4. The work was published without a copyright notice prior to the effective date of the new copyright law in 1988.
This has been occasionally annoying when I've had to pass up some useful piece of code because it's questionable whether anyone can use it without incurring liability (distributing someone else's property makes me an accessory to infringement).

Since SNIPPETS includes both public domain and free code, be sure to carefully read each header for any free license restrictions which may apply.

Portions of SNIPPETS are from my own MicroFirm Function Library (MFL). These are provided for free, subject to the license restrictions specified in the LICENSE.MFL file. Briefly, it says you can use the source files to create programs for any use, even commercial distribution, but you can't sell the source itself or object files (alone or in libraries) built from the source, nor can you remove the copyright notice.

Sharp-eyed users will note that the pages of this web site each carry a copyright notice. This simply means that the web pages used to navigate this site as well as instructional text are copyrighted. The code remains free!


SNIPPETS special thanks to...

Auke Reitsma Jon Guthrie Jerry Coffin Ray Gardner Dave Burton

...without whose assistance and supererogation, SNIPPETS would not be what it is today!


Jens M Andreasen Scott Andrews Erik Bachmann Ed Beroset
Ed Blackman J. Blauth Carey Bloodworth Walter Bright
Gary S. Brown Jon Burchmore Doug Burger Joseph Carnage
Joe Celko Dave Chapman Sreenath Chary Andrew Clarke
Jerry Coffin Fred Cole Marty Connely Eric Coolman
Mark Corgan Ross Cottrell Carl Declerck Mike Demski
Brian Dessent DECUS Michael Dehlwes Pieter de Jong
Mark Devlin Mike Dodd Bob Dolan Scott Dudley
Rob Duff Jeff Dunlop Paul Edwards Jay Elkes
David Engel David L. Fox Jeffery Foy Jeff Galbraith
Ray Gardner David Gersic David Gibbs gk
D. Goodenough Jerry Gore J-Mag Guthrie Jon Guthrie
Wayne Halsdorf Wayne Hamilton Dave Hansen David Harmon
Blair Haukedal K. Heidenstrom Dan Hoey Maynard Hogg
Jerry Houston Larry Hudson Joe Huffman Matthew Hunt
Shamim Islam Bob Jarvis Ted Jensen Thor Johnson
Paul Johnston M. Jones Mike Jones Mark Kamradt
Arjen Kentner J. Kercheval Mark Kimes Wayne King
Dave Knapp Don Kneller Gilles Kohl Ed Kowalski
Dan Kozak Kurt Kuzba Jari Laaksonen Scott Ladd
Soleil Lapierre Stephen Lindholm Phil Linttell Lynn R. Lively
John Lots Martin Maney Robert Mashlan Darin McBride
Ray L. McVay Greg Messer Stan Milam Peter Miller
Robert Morgan Craig Morrison Keiichi Nakasato Lynn Nash
Bob Nelson Phi Nguyen VinhHao Nguyen Jeffrey Nonken
David Nugent Jim Nutt S. Offermann Heinz Ozwirk
Curtis Paris Gary A. Parker Ruurd Pels F. Piette
Steve Poole Dustin Puryear Mike Ratledge Auke Reitsma
John Rex Cliff Rhodes Sid Rogers L. Satenstein
Paul Schlyter E. Schneider Roger Scudder Charles Simonyi
Ron Sires Branislav Slantchev Mike Smith Thad Smith
Robert Sprawls Michael Stapleton Mark Stephen Bob Stout
Aare Tali Peter Thomas Ratko Tomic Tom Torfs
Nigel Traves Erik VanRiper Chad Wallace Jim Walsh
Bruce Wedding Bill Wilkie Ross Williams Stephan Wilms
HenkJan Wolthuis Peter Yard Yoshi Martin Zacho


Please support SNIPPETS!

©2001-2007 by Bob Stout, all rights reserved
All source code free as noted.
Tested with: Get Firefox Get MSIE . .

Support: Rapid PHP