Notes about C Compilers
Math408 - Nonparametric Statistics

Return to Math408 page
How to install the MinGW C compiler on your own computer (Windows):

Command-line and IDE C compilers: There are two types of C compilers, each of which has advantages and disadvantages:

(i) Command-line C compilers     and
(ii) IDE or Windows C compilers

To compile and run a C program using a command-line C compiler, you have to go through the following steps:

(i) Write the C program (call it ``myfile.c'') in a text editor or word processor (for example, the simple ``Hello'' program below),
(ii) Save it as a file on your computer's hard disk,
(iii) ``Compile it'' to a computer-executable program by entering a compile command
at a command prompt, for example for the following C compiler programs:

gcc   -Wall   -o myfile   myfile.c      
(using the GNU C compiler, UNIX or Microsoft Windows)
cl   myfile.c
(Microsoft Visual C++ command-line compiler)
bcc32   myfile.c
(Borland C/C++ compiler, Microsoft Windows)

followed by the ``Enter'' key, and finally

(iv) Run the program by entering

myfile

at a command prompt, again followed by ``Enter''. If you want to save the output of ``myfile'' as a text file ``myfile.txt'', enter instead

myfile > myfile.txt

See Command-Prompt Windows below for how to open a command-prompt window on a Windows computer, and also how to get black text on a white background instead of the default white text on a black background. On the Windows computers in the ArtSci computer lab, you can run Math408-Prompt in the Start menu and get a command-prompt window that has been customized for Math408.

In contrast, IDE C compilers have the advantage that all of these steps can be done in one package. (See below.)

The ArtSci computer server has a GNU C command-line compiler on its UNIX system that you can also use over a telnet or telnet-like connection. Anyone taking a course in ArtSci automatically has an ArtSci computer account. In fact, nearly all UNIX systems have a gcc command-line C compiler available.

For Microsoft Windows computers, there is an excellent free command-line GNU C compiler called MinGW (for ``Minimal GNU C for Windows'') that you can download over the Web. The Windows machines in the ArtSci computer center have the MinGW GNU C compiler installed. (As of February, 2005.)  See below for how to install MinGW on a personal computer or laptop.
Warning: Do not confuse the MinGW GNU C compiler with a DOS GNU compiler called DJGCC or DJGPP (in its modern form). The current DJGPP may be a perfectly good C compiler, but I am not familiar with it and it is not as well known.

Another good command-line C compiler is the program cl.exe that comes along with the Microsoft Visual C++ IDE package. This package costs somewhere around $60 with an academic discount.
All three packages (as well as most IDE C compilers) advertise themselves as either C/C++ compilers or as C++ only. However, most work fine as C compilers as well. If the name of your program ends with .c (as in myfile.c), it will usualy be treated as a C program. If it ends with .cpp (as in myfile.cpp), it is treated as C++.

IDE C compilers have the advantage that:

(i) You don't have to use a text editor that is a different program than the C compiler, figure out how to save the file, figure out how to compile and run it, and then figure out where your output went.
(ii)You can compile and run a program by clicking on a single button. The output appears in another window. If there are errors in your program, double-clicking on the error message goes to the location of the error in the text editor window.
(iii)They usually have a built-in editor that is colorized for C. That means that C key words are in one color (like blue), text in text strings may be in another color (like red), comments may be in green, while other text is in black. If you misspell a key word in C or miss a quotation mark, this become immediately obvious.
(iv) IDEs tend to have online documentation.

DOCUMENTATION:  For any C compiler, you will need a good book covering C syntax unless you are already a C expert. Whether you are an expert or not, you should have documentation available about functions in C standard libraries. Most IDEs come with excellent online documentation about C standard libraries. This is lacking with MinGW.

NOTE: The Microsoft Web site (under MSDN) has excellent online documentation for many different aspects of C. There are a few features that may not be in compilers other than Visual C, but this is normally made clear in the online documentation.

IDE Compilers:  The most popular commercial IDE C compilers in the Microsoft Windows world are Microsoft Visual C++ and Borland C++ Builder.  I am not as familiar with the MacIntosh world for C compilers, although I have heard that there are excellent IDE compilers available, nor with recent versions of Borland Builder. Modern MacIntosh computers have a UNIX core, so that you can almost certainly use (free) UNIX command-line C compilers, but you would have to talk to a MacIntosh expert for the details.

Professor Brian Blank (who has often taught Math 1201 in the past) has a C Programming Companion Web page with lots of useful information, including a discussion of MacIntosh C compilers and a list of books for learning C with brief reviews. This Web page does not seem to have been updated since 2000 or so, and parts of it seem dated. For example, the Windows C compiler scene nowadays is much more favorable, and some of the ``DOS'' C compilers mentioned may not work with modern versions of Microsoft Windows.

Disadvantages of IDE C compilers are

(i) Commercial IDE C compilers cost money, for example around $60 for Microsoft Visual C++ .NET with an academic discount. (Check the prices at the WashU bookstore.)
(ii) You may need your own computer to install it on.
(iii) If you have a text editor or word processor that you really like, it may be difficult to use with some IDEs. (This is not a problem with Microsoft Visual C++: If you edit a C program in an outside editor and save the changes, the version in the IDE editor can be instantly updated.)
(iv) Once you get used to using a command-line C compiler and you have found a text editor that you like, using the command-line compiler is much faster than using an IDE.
(v) You can't use an IDE over a telnet interface to a UNIX computer system (like ArtSci).

C standards: C89 and C99:

Some C compilers (like Visual C) are basically C89 ANSI or C89 ISO standard C compilers. This means that they (mostly) follow C standards set in 1989-1990. The MinGW command-line compiler has implemented most of a new C99 C standard that includes a number of useful additional features such as built-in support for complex variables and 64-bit integers, being able to freely intersperse variable declarations and program commands in program code (an absolute no-no in C89 standard C), and other useful odds and ends. C programs that use these additional features may not compile in older C compilers. However, a C89 compiler will be enough for this course. (See below for more detail about C standards.)

Text Editors that You Can Use with Command-line C Compilers (Windows):

Notepad:  This is simplest text editor for Windows computers, and is part of Windows. Notepad in earlier versions of Windows had a number of awkward features, but these have been corrected in current versions of Windows.
Microsoft Word:  DO NOT USE this word processor unless you are an expert in using Word. Word normally saves files with nonstandard (non-ASCII) control codes, for example for font selections or page formatting, that will prevent any C compiler from compiling the program. By default, newer versions of Word capitalize the first letter in nearly every word, which would be disastrous for C keywords. All of these problems have workarounds, but you should be forewarned.
GNU Emacs for Windows: This is a free programming editor that you can download from the GNU Web site. Text for C programs is colorized if you turn on colorization. You can write a C program within Emacs, compile it with an Emacs command that calls a command-line C compiler in the background, and then run your program with its output trapped in an Emacs buffer that you can print or save to disk. With a separate command-line C compiler, this text editor qualifies as a C IDE in itself. (Within Emacs, C colorizing is on the Options menu. The Tools menu gives you command lines for compiling and running programs.)
Windows Emacs is similar to Notepad if you use only the basic features. However, making use of the full power of Emacs is not for the faint of heart. (Another hint: Make sure that you turn on colorizing of marked copy/cut/paste blocks in the Windows Emacs Options menu, since otherwise these will be invisible.)
Crisp and Epsilon:  These are professional programming editors for Windows that cost in the range $100-$200. Crisp (which I am most familiar with) supports colorizing in C. Like Emacs, it can compile C programs within the editor by calling a command-line C compiler in the background, and also run the compiled program within the editor with the output trapped in a file. However, if you keep several Command-Prompt windows open, it is just as easy to switch between windows and do command-line compiles. Crisp is similar to Emacs but has better Windows features.

Text Editors for Use with Command-line C compilers (UNIX):

For UNIX systems, vi or pico or emacs are all good choices. Emacs is a professional programming editor that supports C colorizing as well as compiling C programs within emacs, but standard emacs cannot be used over a telnet interface.

Public-Domain or Free Command-line C Compilers:

MinGW (GNU C): This is an excellent free command-line C compiler for Microsoft Windows. The name stands for ``Minimal GNU (Compiler) for Windows''. This was written by the GNU Free Software Foundation, who also write and maintain the most commonly used C compilers for UNIX systems. A side advantage of this compiler is it will behave in almost exactly the same way as the most common C compilers on nearly all UNIX systems. MinGW not only supports standard C89 C and almost all of C99 C, but the entire Win32 programming interface as well. The latter means that, in principle, if you had the C source files, you could compile Microsoft Word itself.
The MinGW C Compiler, which is called gcc.exe, is installed on the Microsoft Windows computers in the ArtSci computer lab (as of February, 2005).
There are UNIX gcc C compilers on both the ArtSci and the Mathnet computer systems at Washington University. These can be accessed through dial-up or Telnet or Telnet-like connections. (See below.) Anyone who is registered for a course in the School of Arts & Sciences automatically has a free ArtSci computer account.

A Quick Example of Using MinGW gcc on a Windows Computer:
(See below for how to
install (the free) MinGW C compiler on your own personal computer or laptop.)

(1) Using a convenient text editor, write a C program called (for example) myfile.c.  For example, write

#include <stdio.h>
int main(void)
 { printf ("Hello, World!\n");
   return 0; }

(This is the sample program hello.c on the Math408 Web site under Sample C programs.)

(2) Save myfile.c to a convenient folder or directory on the computer, for example c:\temp.

(3) Open a console or Command-Prompt or ``Dos-Prompt'' window (see below). Change the default folder or directory to (for example) c:\temp. Enter

gcc   myfile.c   -o myfile

at the command line (followed by Enter). If myfile.c has been written correctly and the computer can find the MinGW program gcc, a file called myfile.exe will appear in c:\temp.

NOTE: It is much better to compile myfile.c by entering

gcc   -Wall   myfile.c   -o myfile

This will cause gcc to display useful warning messages about potential program bugs. By default, MinGW displays fatal error messages but not warning messages that may indicate other serious program problems. See What to do if your program doesn't run for examples.

(4) Enter

myfile

at the command line to run the program. If you have done everything correctly, then the phrase ``Hello, World!'' will appear on the computer screen. Enter

myfile  >  myfile.txt

to save the output of myfile as the text file myfile.txt.

What Can Go Wrong:

If you get a response like

'gcc' is not recognized as an internal or external command,
operable program or batch file.

then your computer did not know how to find the program gcc from the Command-Prompt window.

Earlier versions of Windows said something like that you entered an unrecognizable or ``Bad command'', and so were presumably an evil person. However, current Windows operating systems are more diplomatic and just say (more or less) that it couldn't find your program.
These error messages means that gcc (or gcc.exe) is not on the command path for computer programs. A command path is a list of folders or directories in which the computer looks for programs to execute from a command line. Other folders or directories are assumed to be off limits, unless you enter an explicit path in front of gcc .

As a quick test for whether gcc is accessible from the command path, enter gcc --version.  You should get a four-line plug for the Free Software Foundation.  If you just enter gcc followed by Enter, then MinGW should say,  gcc: no input files,  but at this stage you may not be able to distinguish one computer complaint from another.

By default, MinGw is installed in the directory c:\MinGW and the program gcc itself is stored in c:\MinGW\bin . You can check by seeing if that folder exists and if it contains a file called gcc.exe .
As a quick fix, if MinGW has in fact been installed in your computer in the folder c:\MinGW, and gcc.exe is in fact in the folder c:\MinGW\bin, then you can add MinGW to your command path by entering

set Path=c:\MinGw\bin;%Path%

at the command line followed by Enter. You can enter Path (following by the Enter key) at any time to see what your command path is. Entering gcc --version or gcc myfile.c -o myfile  should now work.


 

Using gcc on a UNIX server over a dial-up or telnet connection:

The ArtSci server (if you have an ArtSci account) and the Mathnet server (if you have a MathNet account) are both UNIX servers that have older versions of gcc installed. Using gcc here is very much like using the MinGW gcc on a Windows machine except that you should (for example)
(1) First connect to ArtSci using a dial-up connecting program or a Telnet-like program if you are connecting over the Web.
(2a) If you write a C program myfile.c on a personal computer, use a file-transfer program like ftp or fetch or ws_ftp to transfer myfile.c to your ArtSci account.
WARNING: When you transfer text files among UNIX, Windows, and MacIntosh machines, you should try to transfer them in TEXT MODE and not BINARY MODE. (TEXT MODE is the default for ftp, but BINARY MODE is the default for ws_ftp, so that you have to be careful.)
Binary mode transfers preserve the control codes for ends of lines in text files, which is the only difference between UNIX and Windows text files and the main difference between either and MacIntosh files. A well-written program on a UNIX, Windows, or MacIntosh machine should not care where you use a UNIX, Window, or MacIntosh text file. While many programs are like this, others can cause problems. For example, Notepad will neither display nor print a UNIX text file properly. To be safe, be sure that you always transfer text files between UNIX, Windows, or MacIntosh systems in TEXT MODE, not BINARY MODE. (If you have a choice in make a file transfer between systems : If you don't have an option on a MacIntosh system, then it may be making the right choice automatically.)
(2b) Alternatively, you can also write myfile.c within your ArtSci account using a UNIX text editor like vi or pico. However, after you compile myfile.c and create output by (for example) myfile > myfile.txt, then myfile.txt will be a UNIX text file. If you then transfer it back to a Windows system for printing, make sure you do so in TEXT and not BINARY mode. As mentioned above, some (but not all) Windows program will produce bizarre output if you try to print a UNIX text file, as opposed to a native Windows text file.
(3) If your program contains math functions like sqrt or exp or sin or cos or log, then, with most UNIX C compilers, you MUST TELL THE COMPILER EXPLICITLY to look in the compiler's math library. To do this, add the command-line argument -lm to your call to gcc, as in

gcc myfile.c -o myfile -lm

Occasionally C compilers will crash if you add the argument -lm, in which case you should leave it off. With other C compilers (such as MinGW on Windows), it is optional. With the ArtSci and Mathnet UNIX gcc compilers, you must add -lm if your program has any math functions whose code is in the C math library, or else your program will not compile and you will get strange error messages.

If myfile.c compiles successfully, then the executable file name on a UNIX computer is myfile,  not myfile.exe as on a Windows machine. As before, you run it by entering myfile on the command line and then pressing Enter. The command myfile > myfile.txt stores the output of myfile in the text file myfile.txt, again as before.

Remark:

If you enter gcc myfile.c  instead of gcc myfile.c -o myfile  with MinGW (followed by Enter), then (assuming that all goes well) the executable file will be written as a.exe instead of myfile.exe . On UNIX versions of gcc, the executable file is a.out .
This is a pecularity of UNIX-style C compilers. With MinGW, this has the advantage that you can then run the program by entering the single letter a at the keyboard (followed by Enter). Once the output is the way that you like it, you can enter gcc myfile.c -o myfile  to rewrite the program as myfile.exe.

Command-Prompt Windows:

To open a console or Command-Prompt or ``DOS-Prompt'' window on a Microsoft Windows machine, enter for example

Start | All Programs | Accessories | Command Prompt

starting from the Windows desktop. The syntax above means to first click on Start on the lower left-hand corner of the screen, then on All Programs, and so on. The vertical bar | is pronounced ``and then click on''.

The sequence above works on Windows XP.  The sequence will be slightly different on other Windows versions.

When the console window opens, move to a less populated folder if need be, for example by entering  cd \temp followed by Enter.

Taming White-letter-on-Black-background text: By default, Windows command-prompt windows have partially-legible white text on a black background, as opposed to the usual black text on white background that you see in most most books and newspapers (as well as most word processors). You can get the more normal black-text-on-white-background by entering color F0 (followed by Enter) on the command line. (This is ``F-zero'' and not ``F-Oh''. You can remember this by remembering the giant's call ``Fee Fi Fo Fum'' in the children's story ``Jack and the Beanstalk'', but remember to enter ``F-zero'' and not ``F-Oh''.)
The console screen will now be as legible as a word processor screen. To restore the usual nearly-illegible state of most Windows command-prompt windows, enter color 07. (Again, ``zero-7'', as in James Bond, not ``Oh-7.) If you would prefer a nice, Christmassy red text on a green background, try entering color 2c.
Entering color F0 affects the current Command-Prompt window only and has no effect after you close the window. There is a way to make all new Command-Prompt windows on your own computer open with black-text-on-white-background (as in books and newspapers) by making some easy changes to the Windows registry. However, you should be aware that many people discourage ``registry hacks'' on Windows computers except for people who really know what they are doing.

C Compilers in the WashU ArtSci Computer Lab:

The PCs in the ArtSci Computer Lab currently have the MinGW GNU C compiler installed (as of January 2007). (See Notes on C Compilers.). The compiler is installed in the directory c:\MinGW with the compiler itself (gcc.exe) in c:\MinGW\bin. (See also How to Install The MinGW C Compiler on Your Own Computer or Laptop.)

The ArtSci lab computers also have a special console (or Command-Prompt) window that you can call by entering

Start | Programs | Math408-Prompt

starting from the Windows desktop. This opens a window in the easy-to-use directory c:\temp with pleasant black text on a white background, as in Notepad or Word windows, as opposed to the usual less-legible white text on a black background for Command-Prompt windows.

You can check whether the MinGW compiler has been installed properly by running the Math408-Prompt window, entering

gcc

from the keyboard, and pressing Enter. If you get a response something like

gcc: no input files

then gcc is alive, well, and functioning. Note that the message is that gcc is saying no input files,  not that gcc couldn't be found. For a friendlier response, enter

gcc --version

(with TWO HYPHENS before version) at the command line. The computer should respond with the gcc version number and a three-sentence message from the GNU software foundation. That is a definite indication that gcc is alive, well, and waiting for your next command. You can now enter for example

gcc   -Wall   -o myfile   myfile.c

to compile a C program file myfile.c into an executable function or program myfile.exe, as before.

If, instead of gcc: no input files,  you get a message like

'gcc' is not recognized as an internal or external command,
operable program or batch file.

then the computer does not know how to find the MinGW compiler. The easiest fix in that case is to enter

path=c:\MinGW\bin;%path%

from the command line, assuming that MinGW has been correctly installed in c:\MinGW. (Make sure that you enter this command EXACTLY AS STATED.)  This last command adds c:\MinGW\bin to the computer's (actually, that window's) list of directories in which it looks for programs to execute. The MinGW compiler should now work. As a check, enter gcc or gcc --version as above. You should now see the appropriate response that says that gcc is ready and waiting.

If you still get ``computer can't find'' error messages, or in general if you want to see how the Command-Prompt window is thinking, enter

path

from the command line. This will display a list of directories or folders separated by semicolons. If c:\MinGW\bin is among those listed (the path=... command above will make it listed first), then gcc should be available. Make sure that the path string has no syntax errors, for example if the semicolon after c:\MinGW\bin is replaced by a colon.

Note: The option -Wall in

gcc   -Wall   -o myfile   myfile.c

above is not strictly necessary to compile myfile.c,  but is extremely important for detecting potential subtle program bugs. See What to do if your program doesn't run for some examples.


Hints for improving the appearance of any ``DOS-Prompt'' (Command-Prompt) window:

(i) You may find that the window is more pleasant to work with if you change the background color to White and the foreground color to Black. The easiest way to fix this is to enter color F0 on the command line. Alternatively, click on the MS-DOS icon on the upper-left corner of the DOS-Prompt window, then Properties, then Color, then adjust Screen Text to Black and Screen Background to White.
Colors can be specified in this window by RGB (red-green-blue) values. Note that RGB=(0,0,0) is Black and RGB=(255,255,255) is While.
(ii) If the bottom of the DOS-Prompt window is inaccessible, click on the Screen tab of the Properties window above, and change the window height to 25 or 43 lines, or to some value that is small enough so that you can see the bottom of the window.
(iii) You can change the font to (for example) ``8 x 12'' by clicking on the Fonts tab on the Properties window above. This is like to be the default font, but someone else may have changed the font to something strange.
(iv) From the Command-Prompt window, enter

Set prompt=$p$g

(followed by Enter). This will change the Prompt in the Window to something clearer and more pleasant. One time that I was in the ArtSci Computer Lab, there was a text string in the Prompt that continually urged you to close the window by entering Exit.


Installing MinGW on your own computer (Windows):
If you have a personal Microsoft Windows desktop or laptop and want to install MinGW, this is very easy:

(1) The ArtSci computer lab has internet-cable hookups for laptops and luggable desktops, which you can use to get a fast internet connection. If you go to the main MinGW home page on the Internet, it may appear that you have to download 10 different pieces and then somehow integrate them. However, a single downloadable program with all of MinGW C (and potentially C++, Fortran, and Ada as well) can be found at

http://sourceforge.net/project/showfiles.php?group_id=2435

(This is as of December 28, 2006.) Look for a file with a name and size something like

MinGW-5.1.2.exe     (138Kb)

Download this program and run it. It will ask the MinGW Web site to download and/or install the full package. (Warning: Because of the way it works, some computer systems may think that the installer is spyware. If you have a software firewall program, you may have to either give special permission for that program or else temporarily turn off the firewall.)

Run the downloaded program. By default (which you should follow), the full MinGW C package (around 41 Mb) will be installed in the folder c:\MinGW with the compiler program itself (gcc.exe) mostly likely in c:\MinGW\bin.

If you want to learn more about MinGW, their home page is at

http://www.mingw.org

(2) Before you begin using gcc,  you should add the folder c:\MinGW\bin to your command path for computer programs so that your computer will know where to find it, assuming that you followed the installation defaults, and assuming that the current MinGW installer does not do this automatically. See below for how to do this.

(3) If you are interested, see the comments above about improving the appearance of command-prompt windows in Windows.

(4) Documentation for GNU (including MinGW) GCC can be found on the GNU Web site. Note however that this is documentation for using the program gcc --- for example, a comprehensive list of all command-line arguments for gcc --- and is not a C manual. As mentioned earlier, the Microsoft Web site has documentation for many features of C itself. The GNU documentation is written for UNIX users, so that some things will be slightly different on Windows computers.

(5) C89, C99, and added features.
Historical notes: The original version of C (now called K&R C) had a clumsier and more error-prone syntax than current versions of C and is now obsolete. This version of C is called K&R C after the first (1978) edition of Kernighan & Ritchie's book (see the Math 408 Web site), in which C was invented.  This early version of C was vastly improved in 1989-1990, when K&R C was replaced by what is now called ANSI (for American National Standards Institute) or ISO (for International Organization for Standards) C. This is called C89 C or sometimes C90 C.
In 1999, a large number of useful additional features motivated by C++ were proposed and added to the C89 C standard. This version of C is called C99. MinGW GNU C supports a very large subset of these features that includes long long ints (64-bit integers), a complex data type with associated math library functions, and a variety of other useful odds and ends. In contrast, Microsoft Visual C, while it is C 89 compliant, supports only a few C 99 features. See the MinGW documentation (or any C book that covers C99 C) for more details.
(C89 C will be enough for the programs in this course.)

(6) Adding MinGW to your computer's command path:

You can change the command path in any Command-Prompt window (see above), but that is only a temporary fix: Once you close the window, then any changes to the command path will disappear. For a more permanent fix, first make sure that you are in an account that has Administrative privileges. (If you don't know what this means, then you probably are.) Find the ``Environmental Variables'' list in your computer by clicking on (starting from the desktop):

Start | Control Panel | System | Advanced | Environmental Variables

Again, this is for Windows XP, and | stands for ``and then click on''. (If the XP Control Panel is in ``Category View'' instead of ``Classical View'', click on ``Performance and Maintenance'' before clicking on ``System''.)

The sequence above will be slightly different for other Windows versions. The following will work if this is your own computer (which means that you have ``Administrative privileges'' if you want them), but may not work otherwise.
Look at the window that says ``System variables''. Scroll down to and highlight the Path variable and click on Edit. The Path variable is a list of folders on your computer in which Windows by default will look for programs to run. There may be somewhere in the range of 10-20 such folders. Note that the folder names are demarcated by semicolons. (That is, Path is a semicolon-separated list.) Enter   c:\MinGW\bin;   or   ;c:\MinGW\bin   somewhere near the end of this list so that c:\MinGW\bin is part of the semicolon-separated list. (Be extremely careful!) Close all of the open System windows by clicking on OK.
Any new Command-Prompt window should now have a Path with the MinGW program directory. Open a new Command Prompt window and enter ``Path'' on the command line. The new path should appear. Alternatively, enter gcc --version on the command line to see if the system can find gcc.exe. If this does not happen and you have modified the Path variable correctly, then you may have to reboot the computer. (On the Windows computer in my office, the System Path was updated instantaneously for new Command-Prompt windows for my Administrative Account but I had to reboot to use the new Path in my safer Limited Account.)


  • Top of this page
  • Click here for Math408 page
  • Click here to return to Stanley Sawyer's home page
  • Click here for the Mathematics Department home page

    Last modified January 29, 2007