Command-line and IDE C compilers: There are two types of C compilers, each of which has advantages and disadvantages:
To compile and run a C program using a command-line C compiler, you have to go through the following steps:
(using the GNU C compiler, UNIX or Microsoft Windows) | |
(Microsoft Visual C++ command-line compiler) | |
(Borland C/C++ compiler, Microsoft Windows) |
followed by the ``Enter'' key, and finally
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
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.
gcc
command-line C compiler available.
cl.exe
that comes along with the Microsoft Visual C++
IDE package. This package costs somewhere around $60 with an academic
discount.
IDE C compilers have the advantage that:
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.
Disadvantages of IDE C compilers are
C standards: C89 and C99:
Text Editors that You Can Use with Command-line C Compilers
(Windows):
Text Editors for Use with Command-line C compilers (UNIX):
Public-Domain or Free Command-line C Compilers:
A Quick Example of Using MinGW
(1) Using a convenient text editor, write a C program called (for
example)
(This is the sample program
(2) Save
(3) Open a console or Command-Prompt or ``Dos-Prompt'' window (see
below). Change the default folder or directory to (for example)
at the command line (followed by Enter). If
NOTE: It is much better to compile
This will cause
(4) Enter
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
to save the output of
What Can Go Wrong:
If you get a response like
then your computer did not know how to find the program
at the command line followed by
Using
Occasionally C compilers will crash if you add the argument
Remark:
Command-Prompt Windows:
To open a console or Command-Prompt or ``DOS-Prompt'' window on a
Microsoft Windows machine, enter for example
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''.
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
starting from the Windows desktop. This opens a window in the
easy-to-use directory
from the keyboard, and pressing Enter. If you get a response something
like
then
(with TWO HYPHENS before
to compile a C program file
then the computer does not know how to find the MinGW compiler. The
easiest fix in that case is to enter
from the command line, assuming that MinGW has been correctly installed
in
from the command line. This will display a list of directories or
folders separated by semicolons. If
Note: The option
above is not strictly necessary to compile
Hints for improving the appearance of any ``DOS-Prompt''
(Command-Prompt) window:
(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):
(This is as of December 28, 2006.) Look for a file with a name and size
something like
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.)
(6) Adding MinGW to your computer's command path:
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''.)
Last modified January 29, 2007Word
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.
Options
menu. The Tools
menu gives you command
lines for compiling and running programs.)
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.)
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.
gcc.exe
, is
installed on the Microsoft Windows computers in the ArtSci computer lab
(as of February, 2005).
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.
gcc
on a Windows
Computer:
(See below for how to install (the free) MinGW
C compiler on your own personal computer or laptop.)
myfile.c
. For example, write
hello.c
on the Math408 Web site
under Sample C programs.)
myfile.c
to a convenient folder or
directory on the computer, for example c:\temp
.
c:\temp
. Enter
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
.
myfile.c
by entering
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.
myfile
as the text file
myfile.txt
.
'gcc' is not recognized as an internal or external command,
gcc
from the Command-Prompt window.
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
.
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.
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
.
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
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.
gcc
on a UNIX server over a dial-up or telnet
connection:
gcc
installed. Using gcc
here
is very much like using the MinGW gcc
on a Windows machine
except that you should (for example)
Telnet
-like program if you are connecting over the Web.
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.
ftp
, but BINARY
MODE is the default for ws_ftp
, so that you have to be
careful.)
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.
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
-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.
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.
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
.
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
.
cd \temp
followed by
Enter.
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''.)
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
.
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:\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.)
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.
gcc: no input files
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
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
myfile.c
into an executable
function or program myfile.exe
, as before.
gcc: no input files
, you get a
message like
'gcc' is not recognized as an internal or external command,
path=c:\MinGW\bin;%path%
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.
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.
-Wall
in
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.
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.
If you have a personal Microsoft Windows desktop or laptop and want
to install MinGW, this is very easy:
http://sourceforge.net/project/showfiles.php?group_id=2435
MinGW-5.1.2.exe
(138Kb)
c:\MinGW
with the compiler program itself
(gcc.exe
) mostly likely in c:\MinGW\bin
.
http://www.mingw.org
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.
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.
MinGW GNU C
supports a very large subset of
these features that includes long long int
s (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.
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.
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.)