Getting started with C in Windows

Note that MINGW and TextPad are already installed on the PC's in Hicks 213.

Downloading and installing the compiler

Download the mingw-get-inst installer and run it on your computer. When prompted:

Verify that the install has completed successfully by going to the Start menu, opening up the MINGW shell, and typing

gcc -v

...and hitting enter. You should see some version information for the GNU C Compiler, something like this (this output is from my Mac, so yours will look different -- the important thing is that the command runs).

Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5664~105/src/configure --disable-checking
--enable-werror --prefix=/usr --mandir=/share/man
--enable-languages=c,objc,c++,obj-c++
--program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib
--build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10-
--host=x86_64-apple-darwin10 --target=i686-apple-darwin10
--with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5664)

Installing a text editor

You will want to install a text editor that knows how to do C syntax highlighting. I like the shareware program TextPad, but you may also like Notepad++ or even Emacs (note that the last one is quite powerful but has a steep learning curve).

Once you get the editor installed, you can right-click on a C source file in the Windows explorer to associate the .c file extension with your editor of choice.

Testing out your installation

Create a folder for your programs in your Documents folder or on your K: (or H: or whatever) drive. Let's use K:\ccode\example\, for example. Use your text editor to create a file called hello.c containing the following:

#include <stdio.h>

int main(int argc, char** argv) {
  
  printf("Hello, world!\n");
  return 0;

}

Now it's time to compile your program. Open up the MINGW shell from the start menu and type in (substituting whatever drive and path you chose to put the source file in):

cd /k/ccode/example
gcc -o hello.exe hello.c
./hello.exe

You should get the output

Hello, world!

If so, you have successfully compiled your first program!

Note that unlike the DOS prompt (cmd.exe) in Windows, the MINGW shell considers drive letters to be directories, so to change to the C: drive, you would type cd /c instead of C:

Using make to automate compiles

"Make" is a handy utility that recompiles your programs for you by detecting when the source code is more recent than the executable. We will test it by trying to get it to recompile your hello.exe file, changing the source code, and recompiling again. We'll learn more about how to use make in class soon.

In the same directory as hello.c, use your text editor to create a file called makefile (no .c or .txt extension), containing:

hello.exe: hello.c
	gcc -O3 -Wall -o $@ $^

The space at the start of the second line above is a tab character, not 8 spaces (you can enter a tab by hitting the tab key). Note that make won't work unless you indent certain lines with tabs!

If you go to the MINGW prompt, and type

make

you should see a message like

make: `hello.exe' is up to date.

Now change hello.c to print something else (maybe "Hello, world, again!") and save the file. Go back to the MINGW shell and type

make

once more. It will print the line

gcc -O3 -Wall -o hello.exe hello.c

indicating that it has recompiled your program for you.



Back to E15 home page