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].
(written 2015-11-01)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C205 - C and C based languages - Arrays [1614] When an array is not an array - (2008-04-17)
[2002] New C Examples - pointers, realloc, structs and more - (2009-01-20)
[2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3121] New year, new C Course - (2011-01-05)
[3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
[3245] Collections in C and C++ - arrays, vectors and heap memory blocks - (2011-04-12)
[4338] Passing arrays into functions in C - (2014-12-02)
C201 - C and C based languages - C Language Fundamentals [888] Turning C from source to a running program - (2006-10-06)
[1671] Compiling C programs with gcc - an overview - (2008-06-10)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2576] What does const mean? C and C++ - (2010-01-15)
[2842] Staring a C course with Hello World - why? - (2010-06-30)
[3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04)
[3234] Your program - you just provide the filling in the sandwich - (2011-04-08)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3591] Integer types, and integer overflows, in C - (2012-01-25)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
[4555] Preprocessor directives in C and C++ - what they mean - (2015-10-27)
C230 - C and C based languages - Revision of C [4466] Moving from C to C++ - Structured to Object Oriented - a lesson for engineers - (2015-03-28)
Some other Articles
A warm welcome awaits you at Well House Manor, MelkshamLua - changes to how integers and floats are handled - 5.2 to 5.3Moderation - and the tendency to over-moderateJapanese and Malaysian food in MelkshamC - why is slow to write and debug) but fast to run?Allocation of memory for objects in C++ - Stack v HeapPerl, PHP, Python, Lua, Tcl, C++, Ruby - final public courses for 2015Formatting and outputting your own classes in C++Left shift operator on an output stream object - C++Hello World in C++ - a first program, with the process explained