The first step to building C programs is to obtain a C compiler. There are several popular compilers, including the GNU C Compiler and Microsoft Visual C++, but the first step get a compiler is to identify your operating system:
If you are using Linux:
Good for you. To test whether you have GCC, open the terminal and type the command "gcc". If a lot of text about how to use gcc is displayed, chances are you have gcc already. If an error is returned (probably similar to "gcc is not a recognized command"), try the command "yum install gcc", use a package manager to install either "build-essentials" or "Development Tools", check the pages http://gcc.gnu.org/install/configure.html and http://www.faqs.org/docs/ldev/0130091154_71.htm for possible help, or google how to install gcc on your specific Linux distribution. If you are using Ubuntu specifically, check the page http://www.shibuvarkala.com/2009/03/how-to-install-gcc-cc-compiler-in.html for help. Also, there are Integrated Development Environments which make programming much easier, such as Code::Blocks (http://www.codeblocks.org/) or Eclipse CDT (http://www.eclipse.org/cdt/), but these are optional.
If you are using Macintosh:
Like Linux, you can test whether you have a working version of GCC by opening the terminal and typing "gcc". If a lot of text about how to use gcc is displayed, you most likely already have gcc. If gcc is not recognized as a command, you may want to install the Integrated Development Environment xcode if you are using some version of Mac OS X (or possibly a C package for xcode if you already have xcode). information about xcode can be found here: https://developer.apple.com/xcode/. It has a lot of helpful features and I have heard mostly positive comments about xcode. There are several ways to get gcc without xcode, one way can be found here: https://github.com/kennethreitz/osx-gcc-installer.
If you are using Windows:
Microsoft offers a free, express version of its Visual Studio Compiler and Integrated Development Environment here: http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express. Currently, the newest non-beta version is 2010. To obtain Microsoft Visual C++ Express, you may need to create or use a windows live or hotmail account (which are also free). Alternatively, you can obtain a Windows version of gcc by installing mingw(http://www.mingw.org/) which can optionally be used with an Integrated Development Environment like those mentioned for Linux: Code::Blocks (http://www.codeblocks.org/) or Eclipse CDT (http://www.eclipse.org/cdt/).
Now, to test the C compiler, here is a simple C program which prints the phrase "C compiler working" and exits when a key is pressed:
The coloring used in the example is meant to simulate what a IDE will do. Various IDE's use different colors for parts of a program: commands with # before them, things in quotes, words recognized by C (like int and return), written numbers, and several others.
Place the code above into a text file and change the extension to .c (for example, create a file test.c and save the code there). If you are using an IDE, you can create the file in a project and copy the code into the open file. Also, there should be a compile button and a run button for IDE's, so click both (in order) and the program should run.
If you are not using an IDE, use the terminal or command prompt to change the current directory to the location you saved the file with the code (using the cd command). To compile, simply type gcc <file name here>.c -o <program name and extension here>, where program name can be any name (except file name).
Now, to test the C compiler, here is a simple C program which prints the phrase "C compiler working" and exits when a key is pressed:
#include <stdio.h>
int main() { printf("C compiler working"); getchar(); return 0; } |
The coloring used in the example is meant to simulate what a IDE will do. Various IDE's use different colors for parts of a program: commands with # before them, things in quotes, words recognized by C (like int and return), written numbers, and several others.
Place the code above into a text file and change the extension to .c (for example, create a file test.c and save the code there). If you are using an IDE, you can create the file in a project and copy the code into the open file. Also, there should be a compile button and a run button for IDE's, so click both (in order) and the program should run.
If you are not using an IDE, use the terminal or command prompt to change the current directory to the location you saved the file with the code (using the cd command). To compile, simply type gcc <file name here>.c -o <program name and extension here>, where program name can be any name (except file name).
No comments:
Post a Comment