You are on page 1of 4

Running C in Your Computer

CSC 101 Computer Programming


Prepared by CTVicente 06/21/2011

Running C in Your Computer

Running C in Your Computer


CSC 101 Computer Programming

Finding your learn directory or folder


The first step to programming is to navigate your way to the learn directory (or folder) by using the command prompt. Follow these steps: 1) Start a terminal or command-prompt window.In Windows, run the CMD.EXE program, also known as the MS-DOS prompt. This program is on the Accessories or, often, main Programs menu, off the Start button. Or, you can type CMD in the Run dialog box to start the command-prompt window. Change to your home directory. In Windows XP, type this command: cd my documents In other versions of Windows, type this command: cd \My Documents The command prompt should now reflect that youre using the My Documents folder, similar to: C:\Documents and Settings\Dan\My Documents> C:\My Documents> 3) Change to the learn directory. Everyone, type: cd prog/c/learn except for older versions of Windows, where its cd prog\c\learn (Note: the backslashes, not forward slashes.) Confirm that youre in the proper directory. The current directory is displayed, which should look like one of these: C:\Documents and Settings\name\My Documents\prog\c\learn C:\My Documents\prog\c\learn

Running C In Windows
1) Download compiler for C. Many compilers are available in the internet for free. But for this document we will use DevC++ which can be downloaded from the URL below: http://www.bloodshed.net/dev/devcpp.html 2) Download from Sourceforge the compiler with the following specification: Dev-C++ 5.0 beta 9.2 (4.9.9.2) (9.0 MB) with Ming w/GCC 3.4.2. 3) Save the devcpp.4.9.9.2.setup.exe file and install it by the double clicking the file. Preferably store it in the Drive C of your computer to easily follow through this guide.

2)

4)

A Place to Put Your Stuff


When you program you need a place to store your files which include original source code files, the final program files, and perhaps object code files depending upon the compiler you are using. Windows. To create a folder for your C language projects, follow these steps: 1. 2. 3. 4. 5. 6. 7. Open the My Documents icon on the desktop. Choose FileNewFolder to create a new folder and then name the folder prog. Open the prog folder. Choose FileNewFolder to create a new folder, and then name it c. Open the c folder. Create a folder inside the c folder, and name that folder learn. Close the c folder window.

Note: that the common part is the last part, prog/c/learn. If you see that (or prog\c\learn), youre ready to start work. The learn directory is where youre working while you use this guide. Thats where you edit, create, compile, and manage files.

Page 2

Running C in Your Computer

Running an Editor
In Windows you can use the Notepad or you can download Notepad++ as your program editor. Just save the program with a .c file extension.

3) 4)

Inside the Windows folder, you create a batch file program a shortcut to the GCC command used by Dev-C++. You can then use the GCC command at any command prompt in Windows. Carefully type copy con gcc.bat and press the Enter key Carefully type this line: @c:\DevCpp\bin\gcc %1 %2 %3 %4 %5 %6 The line starts with an at sign, @. Thats followed by the full pathname to Dev-C++s GCC compiler, either c:\Dev-C++\bin\gcc. (If you have installed GCC into another folder, put its path there instead; remember to enclose the path in double quotes if it contains spaces!) After gcc comes a space, and then %1, a space, %2, space, and so on. This is important! If you make a mistake, use the Backspace key to back up and correct. If all this seems strange to you, get someone else who better understands Windows (or DOS) to help you.

Example:
#include <stdio.h> int main() { printf(HelloWorld!); exit(0); } /* save as hello.c in the learn folder*/

Compiling and linking


After the source-code text file is created, your next step is to compile and link. This step transforms the meek and mild text file into a robust and useable program on your computer. Read the proper subsection for compiling and linking specifics for your operating system. 5)

Review the line. Double-check everything. Only when it looks like the example in this book do you do the next step. Press the Enter key. Press the F6 key. A ^Z appears on the screen. Press the Enter key.

6)

Making GCC work in Windows


Windows compilers arent designed to be friendly for command-line compiling. Because of that, its up to you to make the compiler work at every command prompt and in every folder in your computer system. One way to make that happen is to create a batch file that runs the GCC (or whatever) command that runs the compiler. It isnt the easiest thing to do, but, fortunately, it needs to be done only once. These steps assume that you have installed the Dev-C++ environment on your PC. Furthermore, they assume that you have installed Dev-C++ into the C:\ Dev=C++ folder. (If you installed Dev-C++ in another folder, you need to make a note of that folders path. For example, if you installed it in the Program Files folder, the path is C:\Program Files\Dev-C++. You must remember the path!) Take a deep breath. 1) Start a command prompt. Note: You know what? Making a shortcut to the MS-DOS window and putting it on the desktop may be a good idea. 2) Change to the Windows folder: cd \windows (Im assuming that Windows is the name of your Windows folder. If not its WINNT or something substitute the folders name for windows in the cd \windows command.)

7)

8)

You see 1 file(s) copied and the GCC.BAT file has been created. Now you need to test the GCC.BAT file, to ensure that its working. Follow the steps listed earlier in this appendix so that youre in the learn folder. (It maybe easier to close the current Command Prompt window and open a new one). When youre in the learn folder, type this command at the prompt: gcc -v If you see a whole lotta blech appear on the screen, congratulations! You gotit to work! If it doesnt work, review the preceding steps. You probably didnt copy the text properly in Step 4, or you could have created the file in the wrong folder. Repeat the steps, and press Y after Step 3 to overwrite the current GCC.BAT file with a new one.

Page 3

Running C in Your Computer

Windows: Compiling, linking, and running


After setting up the GCC.BAT file, youre ready to start creating programs. Eventually, you repeat the following steps often enough that you no longer need to refer to this appendix for help. 1) Ensure that youre in the proper folder. Refer to the section Finding your learn directory or folder, earlier in this appendix. Use your text editor to create your source code file. Type the hello.c program from the previous example. Compile and link the source code. You do this step with the GCC command both steps at once. Heres the command to type: gcc hello.c -o hello You type these four things: gcc, the command to compile and link the source code hello.c, the name of the source code file -o, the output switch hello, the name of the final program If you leave off the -o switch and its option, GCC creates the program file named A.EXE in Windows. I dont recommend it. Instead, remember the -o option and specify a name for the output program. The name can be the same as the source code file. (C language source code files end in .C and program files end in .EXE.) 4) Run the program. Type the program files name at the prompt. For this example, type hello and press Enter. This step executes that programs code and displays something on the screen or does something interesting, depending on what the program is supposed to do.

2)

3)

References: C for Dummies 2nd Edition

Do you see a man skillful in his work? He will stand before kings; he will not stand before obscure men.

God bless!

Page 4

You might also like