C Programming Language
This page is dedicated to the ANSI C Programming language. We've assembled links and copies of interesting or educational articles on C programming, look at Windows Win32 programming, and consider why we still prefer ANSI C to C++.
Contents
- Why we prefer C to C++
- Recommended books on the C Programming Language
- Secure Programming in C
- Win32 Programming using C
- Books on Win32 Programming
- Recommended sites on Win32 Programming
- Avoiding buffer overflows - strlcpy and strlcat
- More on buffer overflows - MSVC's _snprintf
- Some cute programs in C including the first 800 digits of pi in 160 characters of code, Diffie-Hellman in 10 lines of C, and a tricky one-liner by David Korn.
- Free programs and utilities to download
- Interesting articles and links on C Programming
- Quotes from Writing Unmaintainable Code: "Testing is for cowards", "Never, Ever Do Any Performance Testing" and "Never Validate".
Why we prefer C to C++
- C is simpler and more compact
- The reference book is thinner
- It's usually easier to work out what the program's doing
- It generates smaller executables (much smaller in the case of MFC)
- It's more portable
- With good structure and coding practices, it's often just as "safe" as OOP
- Programs written 20 years ago are still just as valid today as they were then
- We're old luddites who hate learning new tricks
We note that many C++ programs we come across are really just plain old C with the
printf
replaced by cout<<.
Where C++ is better
- C++ gives more opportunities for obfuscation
- People know that C++ is a programming language. But "C", well, try doing a web search on that.
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."
- Bjarne Stroustrup
"If C gives you enough rope to hang yourself, then C++ gives you enough rope to bind and gag your neighborhood, rig the sails on a small ship, and still have enough rope to hang yourself from the yardarm"
- Anonymous quote from the The UNIX-HATERS Handbook
C Sharp: Our recommendation is to skip C++ altogether and go straight to C#. You should still learn the basics of C because there is still so much code around written in it. In earlier versions of this page we may have given the impression we were against C# but after carrying out some projects in C# we are converted. Now that it's becoming clearer that it's not just a proprietary Microsoft language (er, well) and that there are free compilers out there, we strongly recommend it. All the advantages of C (well, most) without the buffer overflow and garbage collection problems.
Recommended books on the C Programming Language
- The C Programming Language, Kernighan and Ritchie, 2nd Edition. Still reads well. I still have and use the first edition dated 1978. See <http://cm.bell-labs.com/cm/cs/cbook/>.
- C Puzzle Book, Alan R Feuer, Addison-Wesley, 2nd edition 1999, ISBN 0201604612. The best ever book on understanding the quirks of C and how to use pointers properly.
- The Practice of Programming, Kernighan and Pike, Addison-Wesley, 1999. An update of an earlier book written in the 1970s. Timeless advice, not just on C but on Java, C++ and Perl. See <http://cm.bell-labs.com/cm/cs/tpop/>.
- The Standard C Library, PJ Plauger, Prentice Hall, 1992. Literally how it all works with full source code and tests.
If you read and understand the first three of these books, you will be an expert in C, I promise you. Plauger is a useful reference book that makes up the set. And better, all four of them will span no more than 4 inches on your book shelf.
For more advanced users, try C Interfaces and Implementations: techniques for creating reusable software by David R. Hanson, Addison-Wesley, 1997. The source code and sample pages are available at <http://www.cs.princeton.edu/software/cii/>.
Secure Programming in C
For a good introduction see Secure Programming in C and C++ by Robert C. Seacord, Addison-Wesley, 2005, <http://www.cert.org/books/secure-coding/>. This not only explains clearly what the problems are but provides useful links. For a more detailed work with lots of instantly useable examples for both Windows and Unix, try Secure Programming Cookbook for C and C++ by John Viega and Matt Messier, O'Rielly Media, 2003. The authors provide more information, links and downloads on their site <http://www.secureprogramming.com/>.
Look at the GIAC Secure Software Programmer (GSSP) Certification Exam developed in a joint effort involving the SANS Institute, CERT/CC, several US government agencies. For more information see GSSP Certification Exam. David Ireland was one of the first seven programmers to receive this qualification in C, see SANS Institute press release.
Win32 Programming using C
A personal story: I struggled for months to learn Windows programming with my (then) new copy of MSVC++5. I bought lots of books and followed their examples, but just didn't seem to get it.
Once you'd set up a simple example, such as "Hello World", it was very difficult to make simple changes to create a more complicated example. The object-oriented C++ syntax combined with MFC's attempts to hide the messy bits of Win32 programming made it difficult to trace and understand. Because you'd used wizards to set it all up, you didn't understand why things did what they did. The books all relied on pages and pages of screen dumps of the setup screens to explain what had to be done in a cookbook "follow the recipe" manner. So even if you remembered it perfectly, once you changed environment, you'd be lost again.
The one thing that made it all come together was finding the GENERIC.C example in the Windows SDK (currently at generic.c, unless they've moved it again). Here was a simple example of relatively few lines that worked. What's more, it didn't get complicated by introducing the extra complexities of C++. Better still, you didn't need to use macros to set things up and so could actually alter what you wanted to do without starting again.
The upshot is that the hardest bit about Win32 programming is the first bit. If you understand the basics of the WinMain loop, WM_CREATE, WM_PAINT, and the use of callback functions, you are a long way there. There is a standard wrapper about 30 lines long that you can use. Everything else generally comes down to finding the right Windows functions to use to do what you want (ironically, the best place to find this information and how to use it is often on a VB site!).
Personally I find that programming in plain Win32 C makes for smaller and neater programs. From what we can see, nothing fundamental has changed between Win95 and XP from a standard programmer's point of view. Sure, there are more features and more complexity if you go looking, but the basics are unchanged. A text book on W95 is still quite valid for writing programs that will run on XP. It gets messier when you want to start using WinSock or doing fancy graphics programming. Presumably the next big change will be when the 64-bit variant supersedes the 32-bit one.
Free software: See our free Wclock world time display program
written using straightforward ANSI C and so-called "SDK-C", i.e. the C code used in the Windows SDK.
The source code is available.
New version released
on 1 January 2008.
Recommended books on Win32 Programming in C
- Programming in Windows, Charles Petzold, 5th ed, Microsoft Press, 1999.
- Windows NT Win32 API Superbible, Richard J Simon, Waite Group Press, 1997. There are variants of this book for all varieties of Windows, but they are essentially all the same.
- Windows Graphics Programming, Feng Yuan, Prentice Hall, 2001. The ultimate guide to Win32 GDI and DirectDraw. OK, he uses C++ and MFC, but it's not hard to extract the important bits. Very comprehensive coverage.
Recommended sites on Win32 Programming in C
We have found these sites to be particularly helpful for learning and understanding Win32 programming using the
C programming language.
- theForger's Win32 API Tutorial: Brook Miles's tutorial attempts to get you started developing with the Win32 API as quickly and clearly as possible. You can download the entire site with source code, or get a PDF version. Includes a Spanish translation.
- Catch22 Win32 Tutorials: a collection of programming tutorials for Windows by James Brown. See especially his Win32 Tips and Tricks.
- Al's Programming Resource for Win32: set of tutorials on Win32 programming by Alan Baylis. Uses Flash but he does provide all the example source code separately.
- masmforum website: Tools, toys, tutes and files for assembler programmers. Check the Links to assembler topics.
Avoiding buffer overflows - strlcpy and strlcat
It is not wise to use the ANSI standard strcpy function because of the possibility
of unchecked buffer overflows. The better strncpy function has the
problem that it does not properly terminate the string.
The following strlcpy and strlcat functions are simple
implementations that manage the problems of their original ANSI ancestors.
#include <string.h>
size_t strlcpy(char *d, const char *s, size_t bufsize)
{
size_t len;
size_t ret;
if (!d || !s) return 0;
len = strlen(s);
ret = len;
if (bufsize <= 0) return 0;
if (len >= bufsize) len = bufsize-1;
memcpy(d, s, len);
d[len] = 0;
return ret;
}
size_t strlcat(char *d, const char *s, size_t bufsize)
{
size_t len1;
size_t len2;
size_t ret;
if (!d || !s || bufsize <= 0) return 0;
len1 = strlen(d);
len2 = strlen(s);
ret = len1 + len2;
if (len1+len2 >= bufsize)
{
len2 = bufsize - (len1+1);
}
if (len2 > 0)
{
memcpy(d+len1, s, len2);
d[len1+len2] = 0;
}
return ret;
}
Updated: 27 December 2003 by adding checks for
null strings.
Interestingly, we found a much more complicated version of these functions somewhere on the net. The fact that it had typos in it and wouldn't compile straightaway didn't fill us with confidence. This version is cleaner.
For more on the principles behind their design and advice on their usage see strlcpy and strlcat - consistent, safe, string copy and concatenation by Todd C. Miller and Theo de Raadt.
Windows programmers should refer to Microsoft's Using the Strsafe.h Functions. As with all Microsoft's pages, it may have moved again.
We also like to use this modified version of strlen when users might send us a null string
size_t strllen(const char *string)
{
if (!string) return 0;
return strlen(string);
}
More on buffer overflows - MSVC's _snprintf function
The functions sprintf and Win32 wsprintf have similar problems with buffer
overflow. The snprintf function provided in (recent) Unix implementations like GCC solve the problem admirably.
In MSVC there is a similarly named _snprintf function that you'd expect to do the same thing,
wouldn't you? Wrong! Spot the difference:
Unix: The snprintf() function will write at most size-1 of the characters printed into the output string (the size'th character then gets the terminating `\0'); if the return value is greater than or equal to the size argument, the string was too short and some of the printed characters were discarded. The output is always null-terminated.
MSVC: _snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.
Can you see the problem? In the Unix version, the output is always null-terminated. In MSVC, it's not.Here's a compromise solution we use in MSVC environments. It returns a negative value on overflow instead of the required number but the output is always null-terminated.
#include <stdio.h>
#include <stdarg.h>
int slprintf(char *buffer, size_t count, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = _vsnprintf(buffer, count-1, fmt, ap);
if (ret < 0)
buffer[count-1] = '\0';
va_end(ap);
return ret;
}
Some cute programs in C
Calculate pi to 800 digits in 160 characters of code. Written by Dik T. Winter at CWI.
int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,
f[b]=d%--g,d/=g--,--b;d*=b);}
Calculate the day of the week in 45 characters of code. Written by Mike Keith.
(d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7
Diffie-Helman in 10 lines of code posted anonymously to sci.crypt and publicised by Adam Back. This actually carries out multiple precision modular exponentation using 8-bit digits. Set S to the number of 8-bit digits required plus 1. This example is for 1024 bits.
#include <stdio.h> /* Usage: dh base exponent modulus */
typedef unsigned char u;u m[1024],g[1024],e[1024],b[1024];int n,v,d,z,S=129;a(
u *x,u *y,int o){d=0;for(v=S;v--;){d+=x[v]+y[v]*o;x[v]=d;d=d>>8;}}s(u *x){for(
v=0;(v<S-1)&&(x[v]==m[v]);)v++;if(x[v]>=m[v])a(x,m,-1);}r(u *x){d=0;for(v=0;v<
S;){d|=x[v];x[v++]=d/2;d=(d&1)<<8;}}M(u *x,u *y){u X[1024],Y[1024];bcopy(x,X,S
);bcopy(y,Y,S);bzero(x,S);for(z=S*8;z--;){if(X[S-1]&1){a(x,Y,1);s(x);}r(X);a(Y
,Y,1);s(Y);}}h(char *x,u *y){bzero(y,S);for(n=0;x[n]>0;n++){for(z=4;z--;)a(y,y
,1);x[n]|=32;y[S-1]|=x[n]-48-(x[n]>96)*39;}}p(u *x){for(n=0;!x[n];)n++;for(;n<
S;n++)printf("%c%c",48+x[n]/16+(x[n]>159)*7,48+(x[n]&15)+7*((x[n]&15)>9));
printf("\n");}main(int c,char **v){h(v[1],g);h(v[2],e);h(v[3],m);bzero(b,S);b[
S-1]=1;for(n=S*8;n--;){if(e[S-1]&1)M(b,g);M(g,g);r(e);}p(b);}
Note: For strict ANSI you need to add #include <string.h>
and substitute memset(x,0,S) and memcpy(y,Y,S)
for the bzero() and bcopy() functions above (or write your own loops).
The Best One Liner winner in the 1987 International Obfuscated C Code Contest (IOCCC), written by David Korn and judged "the best one line entry ever received".
main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);}
This should compile without changes on a Unix or Cygwin system.
If you are stuck, here are some
hints to help understand how it works.
Free programs and utilities to download
- Artistic Style: A Free, Fast and Small Automatic Formatter for C, C++, C#, and Java Source Code by Tal Davidson and Jim Pattee
- ITS4: ITS4 (It's The Software, Stupid! -- Security Scanner) is a command-line tool that statically scans C and C++ source code for potential security vulnerabilities and works across Unix and Windows platforms.
- Free Debugging Tools, Program Checkers, Bug Trackers: Christopher Heng's thefreecountry™ links to debuggers, source code checkers (like lint, that scan your source code for suspicious constructs that might be a bug waiting to happen), debugging tools (like strace that traces a system call), API wrappers (that wrap calls to system libraries so that you can check for valid parameters/arguments in system calls), as well as bug trackers which allow you to keep track of bug reports from users and the status of the bugs. There is also a good link to free C/C++ compilers and interpreters.
- Cygwin: Cygwin is a Linux-like environment for Windows.
It comes with the standard GNU gcc compiler.
Particularly useful to test whether your MSVC code will compile on a Linux system
without having one available.
If you're a really anally-retentive C programmer, use this command to compile your code:
gcc -ansi -pedantic -Wall -O foo.c -o foo
(that's a capital letter-O for Oscar, not zero). - Pelles C: Pelles C for Windows is a complete development kit for Windows and Pocket PC.
- Borland bcc32 Compiler: You can download the Borland C/C++ 5.5 command line compiler for free (you may need to fill in few forms). For a quick get-started guide see Using the Borland 5.5 Compiler and command-line tools. For instructions on compiling and linking a Win32 program, see Brook Miles' tutorial.
- Splint - Secure Programming Lint: Splint is a tool for statically checking C programs for security vulnerabilities and coding mistakes. Published by the University of Virginia, Department of Computer Science. A nice utility to know about and claim you use. To be honest, even us most-fussy programmers find it throws up too many false positives.
Some interesting articles
These are interesting articles or links on C Programming that we've stumbled across in the last couple of years. If there wasn't a valid link to the original site, then the articles are posted here as an educational aid. We don't intend to infringe anyone's copyright here. If you are the original author and have a problem with any of these articles being posted here, please contact us and we'll remove it or replace it with a link to your site.
| Interview with Brian Kernighan | Enjoyable reading with some support for our views on C++. Plus advice on how to pronounce his name correctly. |
| comp.lang.c Frequently Asked Questions | Link to www version. Highly relevant. An up-to-date copy may be obtained from ftp.eskimo.com in directory u/s/scs/C-faq/. |
| ANSI C Programming Lanuage | A useful short help on C Programming, including precedence and the preprocessor, by Ross L Richardson. |
| ANSI C Standard Library | Summary of the ANSI C standard library functions. |
| Comprehensive reference for Standard C programming | All the information you need to read and write programs in the Standard C programming language by P.J. Plauger and Jim Brodie. |
| Rationale for C90 ANSI C | A link to the original Rationale for American National Standard for Information Systems - Programming Language - C. This was included in the original ANSI standard explaining why things were done as they were. It was unfortunately dropped from the ISO standard. |
| C99 Standard | The last public draft of the C99 standard (ISO/IEC 9899:1999) is available from the JTC1/SC22/WG14-C site as well as the Technical Corrigenda and the Rational for the C99 standard. |
| Style in C | Rob Pike's set of essays on coding style and a "philosophy of clarity in programming" as opposed to laying down hard rules. |
| C Traps and pitfalls | An essay by Andrew Koenig of AT&T Bell Labs written as the basis to his book C Traps and Pitfalls. "The C language is like a carving knife: ... This paper shows some of the ways C can injure the unwary, and how to avoid injury." Worth reading (and then re-reading). |
| Incompatibilities Between C and C++ | David R. Tribble's detailed listings of incompatibilities between ISO C and ISO C++. |
| How To Steal Code | Henry Spencer's article on inventing the wheel only once. "Never build what you can (legally) steal! Done right, it yields better programs for less work". There are some useful C programs at the end. |
| The Ten Commandments for C Programmers | Henry Spencer's amusing, but true, adaptation of the ten commandments for C programmers. We love number 6: If a function be advertised to return an error code in the event of difficulties, thou shalt check for that code, yea, even though the checks triple the size of thy code and produce aches in thy typing fingers, for if thou thinkest ``it cannot happen to me'', the gods shall surely punish thee for thy arrogance. Read and take note. |
| How to write unmaintainable code | A link to Roedy Green's very amusing article on how to guarantee a lifetime's employment by making life impossible for maintenance programmers. (He actually has to explain that it's a joke!) Although focused on Java, he gives plenty of examples in C. If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. More quotes below. |
Quotes from How to write unmaintainable code
Testing is for cowards: A brave coder will bypass that step. Too many programmers are afraid of their boss, afraid of losing their job, afraid of customer hate mail and afraid of being sued. This fear paralyzes action, and reduces productivity. Studies have shown that eliminating the test phase means that managers can set ship dates well in advance, an obvious aid in the planning process. With fear gone, innovation and experimentation can blossom. The role of the programmer is to produce code, and debugging can be done by a cooperative effort on the part of the help desk and the legacy maintenance group.
Never, Ever Do Any Performance Testing: Hey, if it isn't fast enough, just tell the customer to buy a faster machine. If you did do performance testing, you might find a bottleneck, which might lead to algorithm changes, which might lead to a complete redesign of your product. Who wants that? Besides, performance problems that crop up at the customer site mean a free trip for you to some exotic location. Just keep your shots up-to-date and your passport handy.
Never Validate: Never check input data for any kind of correctness or discrepancies. It will demonstrate that you absolutely trust the company's equipment as well as that you are a perfect team player who trusts all project partners and system operators. Always return reasonable values even when data inputs are questionable or erroneous.Quotes from How to write unmaintainable code by Roedy Green.
This page last updated: 1 May 2008
To comment on this Contact
DI Management.
Return to the Tips page.
[Top]