Thursday, May 16, 2013

Chapter 1: Strengths and Weaknesses

I'm having one of those days!! I told myself this morning that I would reward myself with note-taking today if I could finish all of my work. Well, that was peachy, except I haven't been able to find a definitive "end" to my work! Something new skids across my desk every time I finish something else and I didn't even turn my OWN computer on until after noon. Blaargh!

I really wanted to crack open the book and read more after I got home last night, but Guild Wars 2 ate up my free time. It'll be ToR tonight, because I'll want to keep my sicky honey company if he's up to playing. He's got a bad ear infection. :(

He admitted to me this morning that he may put forth a little more effort into learning code. That makes me happy! But it could have just been the meds talking. ;) Heeeehehe.

ANYWAY. You're not here for my BABBLING. Part two of Chapter 1 is beyond the break!




"Like any other programming language, C has strengths and weaknesses. Both stem from the language's original use... and its underlying philosophy." -K.N. King

This philosophy is detailed below.

C is a low-level language, which is known sometimes as being "close to hardware". Some other programming languages will disguise or hide these machine-level concepts, but because it was used to write operating systems originally, C needed to allow the writer this closeness. It is also very fast, as the operations used typically correspond closely to a computer's built-in processes.

C is very small. The reference manual in K&R covers the entire language in only 49 pages. This is helped by the heavy reliance upon "libraries" of standard functions. The libraries are what store the commands and processes, and are accessed by using header files. These header files allow for the program to be organized and stored in a way that a process is only used when it needs to be.

C is permissive. It assumes you know C, and provides flexibility and freedom in that respect. It also does not require error-checking (though, I'm finding in my own adventures that it's not a bad idea to have a program that will catch that missing semicolon...).

(As I pause here to take a sip of coffee, only to find the fly I swatted away from my face has drowned in my cup...)

The strengths of C speak for its popularity.


  • The language is very efficient. Looking back at its history, it needed to be able to run on limited amounts of memory; because of its low-level nature, and the use of operations that are so close to what a computer already uses, very little memory is lost decoding the commands issued in C.
  • It is very portable. This wasn't necessarily intended, but it certainly helped to fuel C's popularity. Its early association with UNIX and the publication of the ANSI/ISO standards also helped, as the language does not have multiple dialects (although this was threatened by that period of time prior to the publication of those standards). C compilers are small and easy to write, giving them great accessibility. C, itself, was written to be portable, and has features to support that.
  • C's collection of data types and operators make it very powerful.
  • The ultimate lack of restrictions make C flexible, and not only for systems programming. C is used for all sorts of applications, in all kinds of fields (just one example I found with a quick Google search). It allows for operations that would be illegal or impossible in other languages, which can make things easier but can also allow for bugs to slip through.
  • The standard library is one of C's biggest strengths, as it contains hundreds of functions that can be used for input/output, string handling, storage allocation, among others (reference my link above about header files).
  • Of course, the integration with UNIX makes C a very strong language. UNIX goes as far as to assume the user already knows C, and there are some tools that are dependent on this.


As there are strengths, there are also weaknesses. Most of them come from the same closeness to the machine that was referenced as a benefit. For example:


  • C programs can be prone to errors. Much like assembly language, errors often go unnoticed until the program is run, as compilers usually don't detect them. The author warns of pitfalls that can catch unwary coders, and thankfully, we'll be seeing things like how extra semicolons could cause an infinite loop within a program later on.
  • C programs can be confusing. The author puts this really well, but I don't want to copy out his explanation word-for-word. Essentially, because it has features that aren't found in all programming languages, these features are often misunderstood. Additionally, they can be combined in hundreds of ways, many of which can be hard to understand for anyone but the person who wrote them. The flexibility of the language adds to this; if a programmer wants, it is possible to combine the features of C so cleverly that the resulting program is overly convoluted and almost impossible to understand. In fact... There is an entire competition based on writing the most confusing C programs possible. I love the program the author provides as an example -- the winner from 1990's "Best Small Program":
    • v,i,j,k,l,s,a[99];
    • main()
    • {
    •      for(scanf("%d",&s);*a-s;v=a[j*=v]-a[i],k=i<s,j+=(v=j<s&&(!k&&!!printf(2+"\n\n%c"-(!1<<!j),"....
    • ...and so on. The program was written by Doron Osovlanski and Baruch Nissenbaum, and the entire program prints all the solutions to the Eight Queens problem in chess. (I'm in awe...)
  • Moving onward with the weaknesses of C, it can be painfully difficult to modify. The larger the program, the harder it is, if maintenance hadn't been kept in mind during development. Other languages usually provide features such as classes and packages that support the division of large programs into more manageable pieces that are easier to upgrade. C does not.


There is a short list the author provides for learning to effectively use C. I'm running short on time, so I will list it here in abbreviated form:
  • Learn how to avoid C pitfalls. Additional reading on this subject: C Traps and Pitfalls by Andrew Koenig.
  • Use software tools to make programs more reliable. For example, lint.
  • Take advantage of existing code libraries. I mean, they're out there; you might as well.
  • Adopt a sensible set of coding conventions. This is kind of like your style, but also helps to make sure you don't go making a confusing program unless you really want to.
  • Avoid "tricks" and overly complex code.
  • Stick to the standard.
All right, if I'm going to get home at a decent hour, I have to run out and do deliveries. I was hoping to go into some more detail about some of these things, but for now, this will have to do. 'Til next time!

No comments:

Post a Comment