Main Content

C - why is slow to write and debug) but fast to run?

Archive - Originally posted on "The Horse's Mouth" - 2015-11-01 07:35:29 - Graham Ellis

I often say on my courses that you can write code in a morning in Perl, Python or Ruby that would tae a week in C. How come, and why would anyone use C these days?

Firstly, C is the underlying language on which others are built. We need it there in the background, even if we don't (ourselves) code in it. It's used for the operating system, for the other language compilers ... very much like a building needs foundations, but even the keenest DIY experts will rarely mess with foundations.

Secondly, C is very fast when it (eventually!) runs. The language trusts the coder to get things right, rather than imposing checks and balances at run time. And if your code needs to run very fast, and be compact, you'll never be able to do better than optimised "C" if you're using an alternative language that's written in C itself.

We teach C anc C++. For the right applications, they remain required languages and we're a niche provider; it may be that you need to do huge numbers of calculations, to maintain existing code, to work with embedded systems, device drivers, etc ... and I am happy to teach you, even if you feel you're doing an awful lot of work compared to other languages.

A new example of what you need to check for / what can go wrong in C has been uploaded [here] on our web site ... including a plea to readers to note that the code is a series of examples of what not to do!

So - what are these thsings that I've done in my example?

• I've used a short ints to hold numbers which when added up exceed the range of a short int - resulting in the calculated value wrapping round and being incorrect

• I've made use of an uninitialised variable - resulting in an undefined value being used which is likely to vary each time I run my program

• I've accessed an array beyond its declared scope - resulting in goodness only knows what data being changed and presented; I took this example even 'wider' off the array when I first wrote it, and my program "hung" - probably in an infinite loop as I suspect I was resetting the variable that contained my loop counter every time around the loop

• I've accessed memory through an undefined (uninitialised pointer) resulting in me choosing to work at a random position. In my sample run, I just got a silly values out, but I could have got all sorts of bus or segmentation errors.

• I've printed out a floating point number using an integer conversion - and the result is based on the bit pattern of the float and not simply the whole number part of the float. at least my compiler gave me a warning about this one!

• I've tried to alter a character string that can't be altered. No compile time error, but a bus error when I ran the code.

In an example that's set up this way, it's easy enough for me to show you the problems and explain them - but it can be quite a different thing for a newcomer to C to avoid making any of these errors - or spot them if they have been made and need correcting - in a practical programming environent. So C coding calls for time, testing, discipline on the part of the programmer, and the splitting of code into a series of managabale functions rather than running as larger blocks.

For details of our C and C++ courses, see [here].