Examples for _21st Century C_ by Ben Klemens
Switch branches/tags
Nothing to show
Clone or download
machste and Ben Improve make_bit example
Use the '-z' option of the 'test' command to check whether 'MSG' is
a string of zero length.

Do not test the variable 'MSG' twice, return '1' by using the
'false' command in the first 'if' statement. In this way the 'push'
target is aborted right after printing the reminder. There is no
need to test it once again.
Latest commit ecfa557 Dec 24, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
py Mostly Python Distutils update Aug 25, 2014
tutorial Split anonymous structs into two examples Sep 20, 2014
README.md Highlight asprintf.c more May 23, 2018
amort_interface.c Copy the code from the previous repo Jan 13, 2014
amort_use.c Copy the code from the previous repo Jan 13, 2014
amortize.c Copy the code from the previous repo Jan 13, 2014
amortize.h Copy the code from the previous repo Jan 13, 2014
arithmetic.c Copy the code from the previous repo Jan 13, 2014
asprintf.c fix resource leaks Jun 8, 2018
auto.conf Copy the code from the previous repo Jan 13, 2014
automem.c Copy the code from the previous repo Jan 13, 2014
benford.sh Copy the code from the previous repo Jan 13, 2014
boxes.c Copy the code from the previous repo Jan 13, 2014
c_factors.c _Atomic (prime_s) -> _Atomic prime_s May 22, 2015
c_primes.c Checking whether i is prime requires only searching for factors up to… Oct 9, 2017
cetology.c Copy the code from the previous repo Jan 13, 2014
cetology.make Copy the code from the previous repo Jan 13, 2014
charct.c Copy the code from the previous repo Jan 13, 2014
complex.c Copy the code from the previous repo Jan 13, 2014
const.c Copy the code from the previous repo Jan 13, 2014
constchange.c Copy the code from the previous repo Jan 13, 2014
constfusion.c Copy the code from the previous repo Jan 13, 2014
conststruct.c Copy the code from the previous repo Jan 13, 2014
copystructs.c Copy the code from the previous repo Jan 13, 2014
copystructs2.c Copy the code from the previous repo Jan 13, 2014
cplx.h Copy the code from the previous repo Jan 13, 2014
curly.c Copy the code from the previous repo Jan 13, 2014
dict.automake Copy the code from the previous repo Jan 13, 2014
dict.c fix memory leak May 31, 2015
dict.h Copy the code from the previous repo Jan 13, 2014
dict_test.c Rm commented out code. Feb 20, 2016
dict_use.c Copy the code from the previous repo Jan 13, 2014
dynamic.c Fix dynamic.c segmentation fault May 9, 2017
erf.c Copy the code from the previous repo Jan 13, 2014
errortuple.c Copy the code from the previous repo Jan 13, 2014
fibo.c Copy the code from the previous repo Jan 13, 2014
find.moby Copy the code from the previous repo Jan 13, 2014
floatfail.c Copy the code from the previous repo Jan 13, 2014
fstr.c Copy the code from the previous repo Jan 13, 2014
fstr.h Copy the code from the previous repo Jan 13, 2014
gcov.sh Copy the code from the previous repo Jan 13, 2014
gdb_showlist Copy the code from the previous repo Jan 13, 2014
getenv.c Copy the code from the previous repo Jan 13, 2014
getstrings.c Copy the code from the previous repo Jan 13, 2014
glist.c Copy the code from the previous repo Jan 13, 2014
groupabm.w Copy the code from the previous repo Jan 13, 2014
groups.h Copy the code from the previous repo Jan 13, 2014
groups.w Copy the code from the previous repo Jan 13, 2014
gsl_distance.c Split anonymous structs into two examples Sep 20, 2014
gsl_erf.c Copy the code from the previous repo Jan 13, 2014
ideal.c Copy the code from the previous repo Jan 13, 2014
ideal.h Copy the code from the previous repo Jan 13, 2014
iftest.sh Copy the code from the previous repo Jan 13, 2014
iggy_pop_detector.c Copy the code from the previous repo Jan 13, 2014
intptr.c Copy the code from the previous repo Jan 13, 2014
keyval.c Copy the code from the previous repo Jan 13, 2014
keyval.h Copy the code from the previous repo Jan 13, 2014
linecount.sh Copy the code from the previous repo Jan 13, 2014
macro_varargs.c Copy the code from the previous repo Jan 13, 2014
make_bit Improve make_bit example Jun 17, 2018
mapreduce_wc.c Update the threading chapter Sep 22, 2014
memmove.c Copy the code from the previous repo Jan 13, 2014
mmap.c Copy the code from the previous repo Jan 13, 2014
mutex_wc.c Copy the code from the previous repo Jan 13, 2014
na.c 2nd ed formatting fixes Jul 2, 2014
noreturn.c 2nd ed formatting fixes Jul 2, 2014
nyt_feed.c Copy the code from the previous repo Jan 13, 2014
olden_varargs.c Copy the code from the previous repo Jan 13, 2014
openmp_atoms.c Update the threading chapter Sep 22, 2014
openmp_factors.c Remove do-while wrapper for a safer Stopif macro Apr 13, 2014
openmp_getmax.c Add additional compilation notes Sep 25, 2014
openmp_wc.c Update the threading chapter Sep 22, 2014
papersize.c Copy the code from the previous repo Jan 13, 2014
pointer_arithmetic1.c Copy the code from the previous repo Jan 13, 2014
pointer_arithmetic2.c Copy the code from the previous repo Jan 13, 2014
preprocess.c Copy the code from the previous repo Jan 13, 2014
print_dispatch.c Add examples for function dispatch Apr 11, 2014
print_methods.c Add examples for function dispatch Apr 11, 2014
print_typedef.h Add examples for function dispatch Apr 11, 2014
print_vtable.c Add examples for function dispatch Apr 11, 2014
print_vtable.h Add examples for function dispatch Apr 11, 2014
print_vtable_use.c Add examples for function dispatch Apr 11, 2014
process_dir.c Copy the code from the previous repo Jan 13, 2014
process_dir.h Copy the code from the previous repo Jan 13, 2014
pthread_factors.c Update the threading chapter Sep 22, 2014
pthreads.c Copy the code from the previous repo Jan 13, 2014
pthreads.make Copy the code from the previous repo Jan 13, 2014
regex.c Add regex parsing example Apr 11, 2014
regex_fns.h Add regex parsing example Apr 11, 2014
sadstrings.c Copy the code from the previous repo Jan 13, 2014
safe_sum.c Copy the code from the previous repo Jan 13, 2014
sasprintf.c Copy the code from the previous repo Jan 13, 2014
seamlessone.c Copy the code from the previous repo Jan 13, 2014
seamlessthree.c Split anonymous structs into two examples Sep 20, 2014
seamlesstwo.c Split anonymous structs into two examples Sep 20, 2014
show_tree.c Copy the code from the previous repo Jan 13, 2014
simple_cplx.c Copy the code from the previous repo Jan 13, 2014
sizesof.c Copy the code from the previous repo Jan 13, 2014
stddev.c Simplify stddev.c compliation Jun 17, 2018
stddev_bugged.c Simplify stddev.c compliation Jun 17, 2018
stopif.h Remove do-while wrapper for a safer Stopif macro Apr 13, 2014
string_utilities.c Copy the code from the previous repo Jan 13, 2014
string_utilities.h Copy the code from the previous repo Jan 13, 2014
strtod.c Add additional compilation notes Sep 25, 2014
sum_to_nan.c Copy the code from the previous repo Jan 13, 2014
times_table.c Add additional compilation notes Sep 25, 2014
uint.c Copy the code from the previous repo Jan 13, 2014
unicode.c Split anonymous structs into two examples Sep 20, 2014
unictr.c Copy the code from the previous repo Jan 13, 2014
unictr.h Copy the code from the previous repo Jan 13, 2014
varad.c Copy the code from the previous repo Jan 13, 2014
vectorize.c Copy the code from the previous repo Jan 13, 2014
wordcount.c Remove do-while wrapper for a safer Stopif macro Apr 13, 2014

README.md

21st-Century-Examples

Dear Reader,

Here are all of the examples from the second edition of 21st Century C by Ben Klemens [ http://tinyurl.com/c-for-moderns-2 ]. You'll notice that the captions for all of the examples in the text end in a filename in parens, like (string_utilities.c); that name is referring to the file here.

  • Each .c file has a note about how to compile it. Some involve a makefile, and some have a command or two to cut/paste onto your command line. dict_use.c includes an extensive script that runs autoconf on the system.

  • Everything is UTF-8. If you are using Windows and the encoding becomes relevant, try iconv -f UTF-8 -t UTF-16 < unicode.c > unicode.16.c

  • I assume you have the requisite libraries in place, including the GSL and GLib. If installing via package manager, don't forget the -dev or -devel packages. One or two of the examples require Apophenia (available from http://apophenia.info), CWEB, or other requisites which should be obvious from the header, and you can't run the Autotools examples without having Autoconf, Automake, and Libtool installed.

  • All of the CFLAGS use the GCC's -std=gnu11 flag, requesting that GCC use the C11 standard. If you have an older copy of GCC that predates C11 support, you'll need to change every instance to -std=gnu99. There exist other compilers that don't understand this flag, and you'll need to delete it entirely.

Here's the GNU sed command to do the changes on every .c file in the current directory all at once:

#For a version of GCC that predates C11:
sed -i -e 's/-std=gnu11/-std=gnu99/g' *.c

#For Clang and icc, which don't need the -std=... flag at all:
sed -i -e 's/-std=gnu11//g' *.c
  • If you get linker errors about resolving the (common but not C-standard) function asprintf, use the version provided as an example in asprintf.c, and the function declaration in that file. Or use the libiberty library, which means adding the -liberty flag to your LDLIBS. For example, MSYS includes libiberty as part of its development package.

Have fun with them,

Ben Klemens July 2012