Programming C on Windows

Introduction

In this article, I will look at the possibilities of basic C development on the Microsoft Windows platform. Current standard C is not a first-class citizien there, but since their preferred programming language, C++, is relatively compatible to C, it is nonetheless possible to compile programs written in C.

I prefer to use a text editor and command line utilities to write programs in C, because one has a much better feel for the ecosystem than by using a full blown IDE. This is the case on the Microsoft Windows platform in particular, where the “classical” IDE is Microsoft Visual Studio, which has especially bad support for plain C. Therefore, this article focusses on the Windows SDK and won’t discuss Visual Studio.

Preparation

At first, get the Windows SDK (Downloads for Windows 7 and Windows 8), which contains the compilers and development files. After successfull installation, you should have access to a CMD shell optimized for the Windows SDK. This shell executes a configuration startup script, which sets various environment variables and adds the important tools to your %PATH%-variable. The location depends on the installation parameters, the script from my SDK v7.0 is located at C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\SetEnv.cmd.

After the installation, the following tools should be available on the SDK CMD prompt:

These are the main tools used in basic C development, the C compiler and the build tool.

Next, we look at a trivial example, which traditionally serves as the first program: the (in)famous “hello, world” program. It is shown in the listing below:

#include <stdio.h>

int main(void)
{
	printf("hello, world\n");
	return 0;
}			

To compile, open the SDK CMD prompt and type cl.exe hello.c. cl.exe is the C-compiler provided by the SDK, and with only a source file given, it compiles this to an executable with the same base name. The output is shown below:

C:\Temp>cl.exe hello.c
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.30729.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

hello.c
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:hello.exe
hello.obj

C:\Temp>hello.exe
hello, world		

Now, your C development environment is set up.

WINAPI and windows.h

nmake