C

C was developed in the early 1970s as a programming language for Unix. It is a statically typed imperative language that was designed to encourage machine-independent programming.

C is generally compiled and compilers are widely available for many hardware platforms and operating systems. Because of this, it has become a baseline for programming in the modern computing environment.

Many languages have been derived from C (either in terms of syntax or semantics), such as C++, Objective-C, PHP, Java and Python to name a few.

Why would I learn this language?

C is a low-level programming language which is useful for writing operating systems and embedded systems. It is often also used for application programming (including game development) and server programming due to its integration with the system and external libraries.

C syntax is generally simple, but its type system often makes source code complex and difficult to read and write.

C is generally available for most platforms, however it is generally hard to write cross-platform applications without using advanced supporting tools.

For all its flaws, C is a popular language and is used throughout the industry, and its syntax is the basis for many other programming languages.

Starting Points

An introduction to C
An extensive tutorial covering many C features.
An Introduction to GCC
A manual that provides an introduction to the GNU C and C++ compilers.
Beej's Guide to C Programming
An introduction to programming C for beginners.

Example Code

The following is an example of the Fizz Buzz problem. You can run and edit this program here.

#include <stdio.h>

int main (int argc, char** argv)
{
    int i;
    for (i = 1; i <= 100; i++)
    {
        if (!(i % 3) && !(i % 5))
            printf("FizzBuzz\n");
        else if (!(i % 3))
            printf("Fizz\n");
        else if (!(i % 5))
            printf("Buzz\n");
        else printf("%d\n", i);
    }
    return 0;
}

The following is an example of the 100 doors problem. You can run and edit this program here.

/* Include printf */
#include <stdio.h>
/* Include malloc and free */
#include <stdlib.h>

void doors (int n)
{
	/* Allocate memory for the array of doors */
	char *is_open = (char*)calloc(n, sizeof(char));
	
	/* Counters for loops */
	int pass, door;
	
	/* Process the doors */
	for (pass = 0; pass < n; ++pass)
		for (door = pass; door < n; door += pass+1)
			is_open[door] = !is_open[door];

	/* Print out the results */
	for (door = 0; door < n; ++door)
		printf("Door #%d is %s.\n", door+1, (is_open[door] ? "open" : "closed"));
	
	/* Free the memory used for the array of doors */
	free(is_open);
}

int main()
{
    /* Call the doors function with n = 100 */
    doors(100);
    
    return 0;
}

Further Reading

comments powered by Disqus