Everything you need to know to get started with C

Everything you need to know to get started with C
HIGHLIGHTS

No language can claim to have had as much of an impact on computing as C has since its inception.

Like most programming languages, C was developed out of dissatisfaction with existing programming languages. Back in 1969 – when the idea of developing an operating system using a higher-level programming language was still novel – Dennis Ritche was beginning to realise the shortcomings of the B programming language as he was working on recoding the Unix operating system to a new architecture in a higher-level language. B was designed for a different computer generation – by Ken Thomson, who also contributed to C. The language was word-addressed instead of byte-addressed, and lacked floating point support. Since the early colonists of the digital era were not spoilt for choice as we are these days, they decided to create a programming language which better suited their needs. 

Thus C was in use as early as 1972-1973, however this was the C before any serious efforts in standardisation were made, and would look odd to today’s C developers. It was only with the publication of the ‘The C Programming language’ by Brian Kernighan and Dennis Ritche in 1989 that C finally got a widely available manual for its syntax. The first ‘official’ C standard that was ratified by the ANSI was published in 1989, and is still available in popular C compilers such as the GCC C compiler. This edition of the language is so popular that it is still the default syntax of the current (4.9) version of GCC.  It is commonly referred to as C89 or C90. Since then C has had two major revisions, once in 1999 (known as C99), and again in 2011 (known as C11). The upcoming 5.0 release of GCC will finally adopt the latest C11 edition of C as the default. 
Given how old it is, it is a testament to the design of the language, and the skill of its designers, that it is relevant to this day.

Learn C tutorial
Dennis Ritche (standing) and Ken Thomson working on the first computer for which they designed C (The PDP-11).

Significance

It is hard to overstate the significance of C in the landscape of computing today. Nearly every popular programming language used today, whether it be JavaScript, Java, C#, PHP, Objective-C, or even Python is inspired by conventions set by C. Compared to languages it inspired though, C is significantly low-level. C improves the readability, maintainability and ease-of-development of code significantly compared to Assembly, and is significantly more portable despite giving developers nearly as much power as Assembly does. C gives developers low-level access to machine hardware, with minimal abstraction and pointers in C let the developer directly work with raw memory locations. As you can imagine, this makes it the perfect candidate for system programming. Few other languages are as suitable, or as highly used in writing low-level code such as operating system kernels or device drivers. One only needs to look at the three major operating system families to see proof of that. The Linux kernel, the OSX kernel (Mach / Darwin) and the Windows (NT) kernel are all mostly written in C, with some Assembly thrown in. In fact when one encounters a kernel not written in C, it is often a point to highlight. 

Another field where C has a significant market share is in programming embedded systems. Low-level systems such as the ones that control various systems in cars, from ABS to managing fuel / air mixtures etc. Since these systems are mission critical, and you wouldn’t want a blue screen of literal death, the version of C used in such systems is often a restricted one. After all, you can’t really patch you car to car 1.0.1 if they detect a bug later on! It is also an incredible point in its favour that C is the language in which the interpreters of popular languages such as Python, PHP, Ruby and Lua are written. The deeper you go into computer programming, the more likely it is that you will find copious amounts of C. Nearly every electronic device you touch, that has some kind of embedded microcontroller or microprocessor in all likelihood runs at least some code that was once stored in a .c file.

Learn C tutorial
Whether its a hobby project or mission critical code running on an embedded device, chances are it was written in C.

Hello World

#include <stdio.h>
int main(void) {
 
printf(“Hello, world!\n”);
  return 0;
}

Hello World is possibly one of the simplest programs you can write in any language, and C is possibly one of the more verbose ones. If you just want something that compiles – with warnings – and throws “Hello, world!” to the screen though, you can even get away with just the following (with some compilers):
main() { 
  printf(“Hello, world!\n”); 
}

Sort

void insertion_sort(int *arr, const size_t arr_len) {
    for (size_t i = 1; i < arr_len; i++) {
        int
elem = arr[i];
        size_t j = i;
        for (; j > 0 && elem < arr[j - 1]; j--) {
            arr[j] = arr[j - 1];
        }
        arr[j] =
elem;
    }
}

Insertion sort in C isn’t exactly complex. Here in the above example we are the newer syntax which allows us to define variable as they are used rather than in the beginning of the function. This has been supported since C99.

Tools and Learning Resources

The age of C has another significant advantage, there is a plethora of tutorials, books, videos, and the like available for C, and many of them are absolutely free.

There are also dozens of compilers and IDEs available for C, most free if not open source. However, of greatest important —for desktop OSs at least— are Microsoft’s Visual C++ compiler, the GNU Compiler Collection (GCC), and the newer Clang. Clang aims to be a drop-in replacement for GCC, and features a similar interface. It is gaining popularity, and is now preferred by a number of BSDs and Apple —which is unsurprising since they are one of its primary developers. If you are using Windows, you have a couple of options. Firstly Microsoft’s C / C++ compiler ships with the Windows SDK and with their Visual Studio IDE. The SDK is free, and a free version of the Visual Studio IDE is available, and has recently expanded its feature-set quite a bit. If you intend to develop purely for Windows, wish to use Windows-specific features, or wish to write extensions for parts of Windows itself, you will need this. Even otherwise, the hallmarks of a well-developed piece of software is its ability to compile under multiple different compilers by different vendors. 

Learn C tutorial
Microsoft’s new Visual Studio Community edition brings the free version of the IDE closer to the paid offering. 

On OSX the development tools are available with the SDK, and can be installed from the OSX DVD, or downloaded via the Mac App Store. This includes the XCode IDE. If you are using Linux, chances are development tools and basic libraries for C are already installed, if not it can definitely be found in your distro’s repositories and is just a command or two away. Also, if you’re using Linux you should probably know how to take it from here! Another good IDE for C and C++ is QtCretor by Digia. It is part of the Qt project, but doesn’t require you to use Qt itself. Best of all, it is free, open source, and cross-platform.

Learn C tutorial  
 XCode is Apple’s free IDE for building OSX and iOS apps. 

If you are using Linux / OSX, C documentation is basically built into your operating system! The documentation for any function is just a single command away. Linux (and Unix and OSX) include the ‘man’ or manual command that can be invoked in the command line to open documentation on any topics. Many people are familiar with the command for looking at the usage instructions for command line programs, but it works equally well for C functions! Try it, if you are running a *nix OS, type ‘man printf’ and find a instant usage guide to that function in C. Most C libraries you install will also install a documentation package, so this should work for any development library you have installed. 

Learn C tutorial  
QtCreator has great debugger integration as well as support for targeting Android. 

Learn C tutorial
The manual that come with most Linux software are an invaluable resource of knowledge. 

If you are using Windows, or otherwise would prefer to view this information in a GUI, you can just try searching for ‘man <function>’ in your favourite search engine, and you will likely find one of the many online man-page repositories. This will be less useful for Windows users though. 
If you’re already reaching for your browser though, there is no end to good learning resources for C. Simply a search will reveal great starting resources, so rather than reiterate those here we will share a three resources that teach something interesting. 

Create your own Kernel Module:
If you ever wanted to get into really low-level programming, building your own driver for the Linux kernel, here is a guide that will help you. Instead of jumping into the deep end, this tutorial showcases a very simple kernel module that simply creates a /dev/reverse device in Linux that will reverse the word order of strings sent to it. Where you take it from there is up to you. 

Learn about Memory Allocators, possibly write your own:
Believe it or not, someone actually has to code the bits of software that actually allocate, deallocate and keep track of the memory used by variables you use in your programs. Since creating and destroying variables happens a LOT in any software, speeding up this process can have a large impact on software performance, especially if the way you use variables is special, for instance if you are developing a game. This article and this one will give an overview of this bit of software.

Add a new feature to Python by modifying its code:
If you’ve ever wondered how the features of a programming language map to actual code in its compiler or interpreter, this article is what you are looking for. It’s written by a core Python contributor, and takes you through adding a new statement to the Python syntax by modifying its C code.  

For tutorials on 15 other hot programming languages go here.

Kshitij Sobti
Digit.in
Logo
Digit.in
Logo