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.


Writing and Compiling C Program in Windows

January 18, 2011

Using Command Line using Visual C++ 2008

  1. Select  Start >> All Programs >> Microsoft Visual C++ 2008 Express Edition >> Visual Studio Tools >> Visual Studio 2008 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
  1. After written the program, to compile it, issue the command cl
C:> cl myprogram.c
  1. During the compilation and linking, myprogram.obj and myprogram.exe will be created.
  2. To run the program:
C:> myprogram

Using Visual C++ 2008 IDE

  1. Create an empty project by select File >> New >> Project. Select General >> Empty Project and type in the project name.
  2. Under folder Source, right-click Select Add >> New Item.
  3. 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
  4. You can program to type your C program. Click Save icon to save your file.
  5. To compile the program select Build >> Compile (Ctrl+F7).
  6. Please note that you cannot run the program until you have build solution.
  7. To build solution select Build >> Build Solution (F7)
  8. Running the program:
  9. Select Debug >> Start without debugging (Ctrl+F5)
  10. For any runtime error, you can debug the program as follows:
  11. Select Debug >> Start debugging (F5)
  12. Please note that during debugging, the program will show and close the console display immediately.
  13. 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 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.

Using Borland C++ Compiler Command Line Tools

  • Installing and running the Command Line Tools
  • Run freecommandlinetools.exe; choose the drive and folder into which you want to install the free C++Builder 5 command line tool development system.
  • From the bin directory of your installation: Add “c:\Borland\Bcc55\bin” to the existing path
  • Create a bcc32.cfg file which will set the compiler options for the Include and Lib paths (-I and –L switches to compiler) by adding these lines:
-I"c:\Borland\Bcc55\include"
-L"c:\Borland\Bcc55\lib"
  • Create an ilink32.cfg file which will set the linker option for the Lib path by adding this line:
-L"c:\Borland\Bcc55\lib"
  • Compiling the program:
bcc32 myprogram.c

I am a Linux Convert

December 7, 2009

I’ve been using Windows operating system since Windows 3.11 and Windows 95. Although I’ve tried out Linux a few times but getting the system up and running is very tedious and cumbersome.  Furthermore, it lacks good web browser and Microsoft Office replacement. That was the time before Firefox and OpenOffice were developed.

Now, the situation has improved a lot. For the past few years, I’ve been trying a few versions of Linux and I must say that we’ve finally got a working operating system with a suite of workable software. Now you could install a Linux operating system together with a suite of free software and use it as replacement for Windows operating system. More and more hardware vendors are also writing drivers that support Linux.

Linux in Enterprise IT

Automation

As a former Windows system administrator, I would appreciate Linux/Unix more due to its automation capabilities. I started to dislike mouse and click features. It is so much more powerful to use the command line with scripting. Using shell script, you can automate almost anything. In fact, Microsoft realized the power of scripting and develop Power Script for Windows server. Windows 2008 servers also come with some basic version that uses only command line instead of Windows.

Scalability

Linux is harder to setup and configures but once it is configured, you could replicate the configuration for multiple servers. In fact, you would appreciate the power of Linux when it scales. Therefore, I think Linux/Unix is good for large enterprise.

Open Source

It is easier for you to study and examine Linux under the hood. You can even study the entire kernel source code. Windows and Unix (non open source) system will always remain a black box. As an IT professional and a hardcore computer geek who likes to learn what’s under the hood; I support Open Source Movement and I prefer Linux over other proprietary Unix operating system.

Support Community

What frustrates me with Windows operating system is that, you could not do anything when system crashes other than re-installation. You need to call Microsoft help desk and find some workaround in the meantime. If you encountered major system problem in Linux, you have a choice of using help desk or you can debug the code line by line since Linux is open source. You can also choose to get help from the Linux forum. I am surprise by the wealth of knowledge and active support from the Linux community. It is easier to search for solution in Linux than in Windows.

Windows Appeal

In the enterprise IT arena, I believe that Linux is the solution. However, I don’t think Windows will lose its market share because Windows has its own appeal. Although you need to restart the Windows server every now and then, it is easier to use and the most important factor is that it is easier to get cheaper worker who knows Windows.

Linux for Enterprise

As an IT Manager, I think that Windows server is more appealing to small and medium companies that lack the scalability. It is easier to get worker who know Windows server at a cheaper rate. For companies that are growing fast and large enterprise, I would recommend Linux.

Linux in Desktop and Personal Computing

Although now Linux has a range of application that could replace Windows operating system, Linux is not going to replace Windows any time soon in the desktop market.

The main reason is the lack of killer apps. In today’s desktop computing, there are a few essential killer applications that could make or break an operating system. They are office productivity suite, web browser, contact manager and PIM (personal information manager), and media content organizer.

Microsoft obviously wins in the category of office suite. OpenOffice is adequate for normal usage and in fact is it recommended for infrequent user.  However, Microsoft Office is still a better quality product compare against OpenOffice.  Furthermore, Microsoft has the advantage of familiarity in usage.

Firefox wins in the category of web browser and it supports Linux. Firefox is one of the reasons Linux is getting more attention. I did not find any winners in the category of PIM and contact. Personally I did not find any PIM software that compels me to switch OS. ITunes is the winner in the category of media content manager. It supports only Mac and Windows.

Among all the killer application, only Firefox supports Linux. As long as there is no application that is better than MS Office and iTunes, Linux would not take over Windows or Mac.

Personally, I am still using Windows as my main computing platform, although I’ve been using Linux more for every other thing such as web browsing. Professionally, I prefer Linux on the server and I also support Windows client operating system.


Tech Tips: Windows Vista – Speed up Booting Time

December 6, 2009

In Windows Vista, you can speed up the boot time by requiring the processor to boot using 2 processor. This is provided you are using Dual Core or Core 2 CPU.

To change the setting, type msconfig under the start search panel.

boot12

Once you have launched msconfig.exe, select <Boot> tab

boot21

Click <Advanced options…>

boot3

In the panel, top left corner, it shows number of CPU with a 1 in the list box. Change it to 2.

boot4

You need to restart the PC.


Tech Tips: Windows – Modify ‘Send To’

December 6, 2009

To modify ‘Send To’, we must get to the ‘Send To’ folder which is located at %APPDATA%\Microsoft\Windows\SendTo

The location %APPDATA% should be at C:\users\<username>\AppData\Roaming

In this ‘Send To’ Folder, you can add any shortcuts you want and it will be included under Send To.