Editing and Compiling C Code
C is what's referred to as a compiled language, meaning you have to use a compiler to turn the code into an executable file before you can run it. The code is written into one or more text files, which you can open, read and edit in any text editor, such as Notepad in Windows, TextEdit on a Mac, and gedit in Linux. An executable file is something the computer can run (execute). The compiler checks the code for errors and, if it seems to be error-free, creates an executable file.
Before we look at what goes into the C code, let's be sure we can find and use a C compiler. If you're using Mac OS X and most Linux distributions (such as Ubuntu), you can add a C compiler to your computer if you install the development tools software for that particular OS. These free C compilers are command line tools, which means you'll typically run them from a command prompt in a terminal window. The command to run one of these C compilers is "cc" or "gcc" plus some command line options and arguments, which are other words typed after the command before you press Enter.
If you're using Microsoft Windows, or you would prefer to use a graphical user interface rather than a command line, you can install an integrated development environment (IDE) for C programming. An IDE is a single interface where you can write your code, compile it, test it and quickly find and fix errors. For Windows, you could purchase Microsoft Visual C++ software, an IDE for both C and C++ programming. Another popular IDE is Eclipse, a free Java-based IDE that runs on Windows, Mac and Linux and has extensions available for compiling C and many other programming languages.
For C, as for other computer programming languages, the version of the compiler you use is very important. You always want to use a version of the C compiler that's as new or newer than the version of the C language you're using in your program. If you're using an IDE, be sure to adjust your settings to make sure the IDE is using your target C version for the program you're working on. If you're at a command line, you can add a command line argument to change the version as in the following command:
gcc –std c99 –o myprogram.exe myprogram.c
In the command above, "gcc" is the call to run the compiler and everything else is a command line option or argument. The "-std" option was added followed by "c99" to tell the compiler to use the C99 standard version of C during its compiling. The "-o" option was added followed by "myprogram.exe" to request that the executable, the compiler's output file, to be named myprogram.exe. Without "-o" the executable is automatically named a.out instead. The final argument "myprogram.c" indicates the text file with the C code to be compiled. In short, this command is saying, "Hey, gcc, compile myprogram.c using the C99 C programming standard and put the results in a file named myprogram.exe." Browse the Web for a complete list of options you can use with your particular compiler, whether it's gcc or something else.
With your compiler installed, you're ready to program in C. Let's start by taking a look at the basic structure of one of the simplest C programs you could write.