C Programming/Why learn C?

C is the most commonly used programming language for writing operating systems. The first operating system written in C was Unix. Later operating systems like GNU/Linux were all written in C. Not only is C the language of operating systems, it is the precursor and inspiration for almost all of the most popular high-level languages available today. In fact, Perl, PHP, Python and Ruby are all written in C.

By way of analogy, let's say that you were going to be learning Spanish, Italian, French, or Romanian. Do you think knowing Latin would be helpful? Just as Latin was the basis of all of those languages, knowing C will enable you to understand and appreciate an entire family of programming languages built upon the traditions of C. Knowledge of C enables freedom.

Why C and not assembly?

edit

The biggest reason to learn C over assembly is because it's much easier and faster to write code in C than in assembly for a given programming task. With C, you will write far fewer lines of code, complete the job much quicker, and with far less mental effort than if you wrote it in assembly. And with today's modern compilers, an executable file compiled from C source code will typically run faster than one written "by hand" using assembly. Only in rare edge cases, and only if you really know what you are doing, can assembly offer important speed advantages over C code compiled with a decent compiler.

And with C, you do not have to sacrifice a lot of low level control over how your code is executed. A typical C statement translates into just a few assembly instructions. But C also provides you with a large software library to help you execute low-level tasks that you'd rather not be bothered programming.

Another huge advantage of C is portability. Different processors have different instruction sets. Having to rewrite and maintain assembly code for each computer architecture you wish to execute your code on is an onerous task. And so one of the main strengths of C is that it combines universality and portability across various computer architectures while still giving you the same kind of low level hardware control you get with assembly. This means you can write your C source code once and easily compile it into binaries for use on a wide variety of machines.

For example, C programs can be compiled and run on the HP 50g calculator (ARM processor), the TI-89 calculator (68000 processor), Palm OS Cobalt smartphones (ARM processor), the original iMac (PowerPC), the Arduino (Atmel AVR), and the Intel iMac (Intel Core 2 Duo). Each of these devices has its own assembly that is completely incompatible with the assembly of any other. C makes it possible to run your code on these machines with much less effort.

So is it any wonder that C is such a popular language?

Like toppling dominoes, the next generation of programs follows the trend of its ancestors. Operating systems designed in C always have system libraries designed in C. Those system libraries are in turn used to create higher-level libraries (like OpenGL, or GTK), and the designers of those libraries often decide to use the language the system libraries used. Application developers use the higher-level libraries to design word processors, games, media players and the like. Many of them will choose to program in the language that the higher-level library uses. And the pattern continues on and on and on...

That said, learning assembly can be fun and worthwhile because it can give you a deep understanding of how your computer works at very low levels. And learning assembly will definitely help you become a more skilled C programmer. So, by all means, we encourage you learn assembly, but when it comes time to do real work, you'll definitely want to get it done with C.

Why C, and not another language?

edit

The primary design of C is to produce portable code while maintaining performance and minimizing footprint (CPU time, memory usage, disk I/O, etc.). This is useful for operating systems, embedded systems or other programs where performance matters a lot (“high-level” interface would affect performance). With C it’s relatively easy to keep a mental picture of what a given line really does, because most of the things are written explicitly in the code. C has a big codebase for low level applications. It is the “native” language of UNIX, which makes it flexible and portable. It is a stable and mature language which is unlikely to disappear for a long time and has been ported to most, if not all, platforms.

One powerful reason is memory allocation. Unlike most programming languages, C allows the programmer to write directly to memory. Key constructs in C such as structs, pointers and arrays are designed to structure and manipulate memory in an efficient, machine-independent fashion. In particular, C gives control over the memory layout of data structures. Moreover dynamic memory allocation is under the control of the programmer (which also means that memory deallocation has to be done by the programmer). Languages like Java and Perl shield the programmer from having to manage most details of memory allocation and pointers (except for memory leaks and some other forms of excess memory usage). This can be useful since dealing with memory allocation when building a high-level program is a highly error-prone process. However, when dealing with low-level code such as the part of the OS that controls a device, C provides a uniform, clean interface. These capabilities just do not exist in most other languages.

While Perl, PHP, Python and Ruby may be powerful and support many features not provided by default in C, they are not normally implemented in their own language. Rather, most such languages initially relied on being written in C (or another high-performance programming language), and would require their implementation be ported to a new platform before they can be used.

As with all programming languages, whether you want to choose C over another high-level language is a matter of opinion and both technical and business requirements could dictate which language is required.