Vancouver Robotics Club - C Programming

Several members of the Vancouver Robotics Club have expressed an interest in learning to program in C. To that end, I have started to collect some resources to help us along this path.

Since many of the platforms that are common at the club, like the AVR, many of the Motorola processors, the ARM processor, are all based around GCC, we'll use a PC version of GCC to ease ourselves into C programming.

The PC toolchain that we'll use, comes from the MinGW - Minimalist GNU for Windows project. If you happen to like using cygwin, then the gcc compiler that comes with cygwin will work just as well. If you don't know what cygwin is, then use the MinGW compiler.

Download Link

Once you've downloaded the executable, run it, and it will come up with a series of dialogs. I chose the defaults for everything, which will put the program in c:\MinGW.

The next step is to add c:\MinGW\bin to your PATH.

  • Windows 95/98

    add the following line to c:\AUTOEXEC.BAT (using Notepad)

         PATH c:\MinGW\bin;%PATH%
    
    You'll need to reboot for this to take effect.

  • Windows NT/2000/XP

    You can edit your PATH using the Control Panel. Open up 'System' and on the Advanced tab, click on Environment Variables. Put c:\MinGW\bin; in front of what's currently there. You can edit either the User or System entry (no need to do both). There is no need to reboot, just start a new command prompt.

Next, you'll need a text editor. If you're already using one that you're comfortable with, by all means, continue to use it. Crimson Editor is a nice little free editor that does syntax highlighting, and will allow us to run the compiler from within the editor. I stashed a local copy over here (1.1 Mb) but it will be faster to download from the Crimson Editor download sites.

For this series of tutorials, we'll put all of our files inside a working directory. So go ahead and create C:\VRC, and inside that, create a directory called "Hello-World". Download (right click and choose "Save As...") Hello-World.c and build.bat into the Hello-World directory.

Start a new MS-DOS prompt. You can use the Run command from the Start menu. Under Windows 95/98 use 'command' (without the quotes), and under Windows NT/2000/XP use 'cmd'. In the listings below, the blue text is what you type, everything else is what you see.

     C:\>c:
 
     C:\>cd \vrc\Hello-World
 
     C:\vrc\Hello-World>build Hello-World
 
     C:\vrc\Hello-World>set CC=mingw32-gcc
 
     C:\vrc\Hello-World>mingw32-gcc -o Hello-World Hello-World.c
     C:\vrc\Hello-World>
If you do a 'dir' command, you should see:
     C:\vrc\Hello-World>dir
      Volume in drive C has no label.
      Volume Serial Number is 903E-1803
 
      Directory of C:\vrc\Hello-World
 
     08/12/2004  06:48 AM    <DIR>          .
     08/12/2004  06:48 AM    <DIR>          ..
     08/12/2004  06:42 AM                39 build.bat
     08/12/2004  06:33 AM               303 Hello-World.c
     08/12/2004  06:48 AM            14,627 Hello-World.exe
                    3 File(s)         14,994 bytes
                    2 Dir(s)   1,397,178,368 bytes free
 
     C:\vrc\Hello-World>
Finally, if you run Hello-World
     C:\vrc\Hello-World>Hello-World
     Hello World
 
     C:\vrc\Hello-World>

You can configure Crimson Editor to run the build.bat to compile your current source file. Lauch Crimson Editor. In the Tools menu, select 'Conf. User Tools..." and fill it in like this:

You should now be able to open a source file, press Control-1 (press the control key and the number one) and it should try to build an executable. You'll see the error messages in the window at the bottom of the editor. Note that this particular batch file only works with programs which are entirely in a single file.

So now you can compile a simple program, and you want to learn more. Here's a really good http://www.le.ac.uk/cc/tutorials/c/index.html course on learning to program in C. During the upcoming meetings we'll go over this material, so if you want to speed ahead, read away.

Another really good introduction to C can be found on this page titled Essential C.

Here's a list of the files that are available for downloading:

   \
build.bat  Batch file to build programs
Hello-World.c  Hello World C file
Sum.c  Sum C file


Home

Copyright 2010 by Dave Hylands