Writing and Compiling C Program using Microsoft Visual C++ 2010 Express Edition

January 19, 2011

Update

For users wanting to compile C program in Microsoft Visual Studio 2012 Express, please refer to the link here http://www.zealfortechnology.com/2013/06/compile-c-program-using-visual-studio-2012.html For users programming in any other platform such as Max OS X or Linux, please refer to my main page here. On the main page, you can also access to my articles about creating static or dynamic library in Windows and other platform.

******************************************************************************************************************

There are two methods to write and compile a C program in Visual C++ 2010. You can compile a C program using command line or you can compile a C program using the Integrated Development Environment (IDE) from Microsoft Visual C++.

Using Command Line to Compile C Program

1. Select  [Start >> All Programs >> Microsoft Visual C++ 2010 Express Edition >> Visual Studio Tools >> Visual Studio 2010 Command Prompt]. Run with administrator privileges.

2. Once you are in the command line, you can use any editor such as edit to write a C program.

C:> edit myprogram.c

3. After written the program, to compile it, issue the command cl

C:> cl myprogram.c

4. During the compilation and linking, myprogram.obj and myprogram.exe will be created.To run the program:

C:> myprogram

Using Visual C++ 2010 IDE

1. Create an empty project by select [File >> New >> Project]. Select [General >> Empty Project] and type in the project name. Alternatively, you can select [File >> New >> Project], follow by [Win32 >> Win32 Console Application]. Type in the project name.

2. If you use Win32 Console Application, you can skip this step. Under [Solution Explorer], right click the project name, and select [Properties]. Select [Configuration Properties >> Linker >> System]. Under [SubSystem], select [Console (/SUBSYSTEM:CONSOLE)].

3. Under [Solution Explorer, Source File], right-click and select [Add >> New Item].

4. Under Code, Select C++ File (.cpp), however, when you type in the name use a program name with extension .c, such as myprog.c and click [Add].

5. You can proceed to type your C program. Click [Save] icon to save your file.

6. To compile the program, under [Solution Explorer, Source File], right click the source file and select [Compile or (Ctrl+F7)].

7. Please note that you cannot run the program until you have build solution.

8. To build solution select [Debug >> Build Solution or (F7)]

9. To run the program without debugging, select [Debug >> Start without debugging or (Ctrl+F5)]. (Note: If you do not have this option, you need to add the command manually under Tools >> Customize. You may add a menu option or a button in the Debug toolbar.)

10. For any runtime error, you can debug the program as follows; Select [Debug >> Start debugging (F5)].

11. Please note that during debugging, the program will show and close the console display immediately.

12. To see the console display, you must run without debugging.

Note:

The problem using Visual Studio is that many files will be created even for a very simple program. All files are created inside the project folder.

Under the project name of your program, the source code and object file is located at sub folder similar to your project name, the executable file is under sub folder <Debug>.

It is simpler to use the command line to compile and run a C program, however, you could use the IDE for writing a properly formatted C program.