Skip to main content

Programming in C - Before We Get Started

Requirements | Framework | POSIX

Recently I have been spending quite a bit of time learning how to program in C. It has been quite a few years since I have had anything to do with C, spending most of my time in a very different OSI layer entirely. Even when I did come across it some time ago, I was never anything but barely competent - this ignorance on my part has always disturbed me, and so I have endeavored to do something about it.

Currently I am taking a few computer science classes at Harvard University; it is my plan to summarize a portion of what I have taken from those classes concerning the C programming language here on my website for readers who are interested.

For readers with an advanced knowledge of C, these readings could be viewed as refresher courses. The content, although filtered through my humble brain, will be entirely the result of knowledge acquired through Harvard, so my hope is that even for the experienced C hacker there may be something of interest.

For the layman, the content should be easily digestible. We will include plenty of links and such for further reading. And as always, readers are encouraged to leave comments or contact me directly should you have any questions about the material covered here.

In order to get started, you will require a software development environment that allows you to compile and execute programs written in C. If your computer runs Linux - congratulations! You already have this. If you have a Windows computer, you should probably download MinGW, which I explain in greater detail below. Mac users should install xcode, which includes the clang compiler and a variety of other development tools. LLVM is another option for users of a variety of operating systems.

You will also need a compiler. With Windows, installing MinGW takes take of this for you by including the gcc compiler compilation with the installation package. For Linux users, I recommend installing either clang or gcc. The commands to do this depend on your distribution. BSD users should already have clang as part of their OS. I've included a couple of command examples below to install clang, or you can check out more comprehensive documentation through the LLVM project.

Redhat::
#sudo yum install clang

Ubuntu/Debian::
#sudo apt-get install clang

Don't I need a special computer to use C?

No! While I use the Red Hat flavor of Linux as my operating system-of-choice for development, you can just as easily use any old computer that you wish. Windows users, for example, have a variety of options available to them to prepare an environment for C. The two options that I prefer, depending on the project, are Cygwin and MinGW.

What is Cygwin?

The developers of Cygwin describe their project as having two primary components. First, Cygwin contains a large collection of GNU and Open Source tools which provide functionality similar to a Linux distribution on Windows. Second, Cygwin has a DLL (cygwin1.dll) which provides substantial POSIX API functionality. Cygwin provides a huge and diverse amount of functionality through its library of third party downloads (way too much to list here).

What is MinGW?

MinGW stands for "Minimalist GNU for Windows". As the name suggests, MinGW provides a very minimal software development interface for native-Windows applications. If you wish to learn how to program C using nothing other than a Windows computer MinGW is absolutely the tool of choice. This is because MinGW requires no third party libraries or other add-ons to get going; all that MinGW needs is the native C runtime library included with Windows, MSVCRT.DLL, and a few other native Windows DLLs.

MinGW ships with a Windows port of the gcc compiler, which lets you compile not just applications written in C, but also C++, ADA and FORTRAN if you are so inclined (C++ is great, but if you are writing FORTRAN applications in 2014, seek professional help immediately). It gives you a linker so you can write your own libraries, an assembler, and an archive manager. 

In summary - for learning how to program C on your Windows computer (especially if you want to create programs that run natively in Windows), MinGW is the tool to use.

What is POSIX? 

POSIX stands for "Portable Operating System Interface". POSIX is a set of standards for applications to ensure interoperability between operating systems.

In theory, an application that is completely POSIX compliant can be run on any OS that is also POSIX compliant. In practice, very few Operating Systems are entirely POSIX compliant. Those that are fully compliant are not widely used outside of big businesses. Some examples of fully POSIX compliant Operating Systems that you may have used or heard of are: Solaris (originally created by AT&T and Sun Microsystems, now owned by Oracle) and HP-UX (Hewlett Packard's proprietary version of Unix). Other examples that are less widely known are AIX, IRIX, RTEMS and A/UX. As you can see, these operating systems tend to share certain characteristics. They are mostly closed source, owned by massive corporations, and designed to work with proprietary hardware. 

Most commonly used Operating Systems are partially POSIX compliant. Most versions of Linux and BSD fall within this group. 

Windows is partially POSIX compliant through the use of a compatibility feature set or a third party application. Cygwin is the application of choice for Windows POSIX compatibility, but it is not the only option. Other applications that provide similar functionality are UWIN (from AT&T Research Labs) and MKS Toolkit (by MKS Inc).  

Windows users may also opt to install an additional subsystem, rather than an application or API, in order to enable a POSIX-compliant environment. Until Windows 2000, Windows NT users could install the frankly-named "Microsoft POSIX subsystem" to enable partial POSIX compatibility. A now-defunct company, Softway Systems, built on top of the Microsoft POSIX subsystem to develop a fully POSIX compliant environment for Windows called Interix. With the relase of Windows 2000, Microsoft provided the Windows Services for UNIX, or SFU (a sly insult to users, perhaps?), also referred to as the Subsystem for UNIX-based Applications or SUA. 

Services for UNIX changed the game quite a bit for developers using Windows, providing a much more functional and less buggy application interface for developers. This is also the current Microsoft-provided POSIX solution as of Windows Server 2012 / Windows 8 (as of this writing Windows 10 is still pre-alpha). As with Cygwin, Services for UNIX provides a whole heap of functionality beyond a simple POSIX-compatible development environment, almost all of which is outside the scope of our discussion. We may return to SFU in the future, as it deserves its own series of posts and explanations. Users are welcome to install this - just follow the instructions provided by Microsoft.

From here on out we will take for granted that our readers have a POSIX-compliant development environment, including the standard C libraries (stdio.h, for example) and a compiler or .