How to Write and Run a C Program in Visual Studio 2010




CVS1.gif

Today I was recalling the good days of the C language and became tempted to play around with POINTERS, STRUCTURES, and DATA STRUCTURES etc. I started to write some C code and as soon as I started, the first hurdle came of where to write the code. I was running on 64 bit Windows 7 machine with Visual Studio 2010. I binged and found many suggestions to download this and install that etc. However being a fan and loyal user of Visual Studio, I was more desired to use the rich IDE of Visual Studio for my C program. To my surprise it is quite possible to use Visual Studio 2010 to write and compile code in C language.
 
In this post I am going to walkthrough writing C program in Visual Studio 2010. Follow the steps as below,

  • Create a new project by clicking File->New->Project.
  • From Installed Template choose other language
  • Choose language Visual C++
  • In Visual C++ choose tab Win32
  • Choose project type Win32 Console Application

See the image below,

CVS2.gif

From the dialog box click on the Next button:

CVS3.gif

The next screen is of Application Setting. You need to make sure that:

  • Application type is set a Console Application
  • In Additional options uncheck the Precompiled Header.

CVS4.gif

After clicking Finish you will find a project that has been created with the following structure. Open the Solution Explorer to see the structure.

CVS5.gif

To start programming, right-click on the Source Files and add a new item. You need to make sure of the following two points:

  • Select C++ File to add
  • But in name change extension to .C, default is .CPP. To work with C language program source file name should be with extension .C. In this case I am giving the source file name as Sample1.C.

CVS6.gif

Now open Sample1.c and write a hello world program as below,

Sample1.c

#include<stdio.h>
#include<conio.h>
void main()
{
         printf("hello C from Visual Stido 2010");
         getch();
}

To compile and run the program, simply press F5 and you should get output in the console window as below:

CVS7.gif

You can see that CSample2.exe is running and this is name of the project.

Next let us go ahead and write some code to print the address of a variable using Pointer.

Sample1.c

#include<stdio.h>
#include<conio.h>
void main()
{
         int number1=9; 
         int *ptrNumber1; 
         printf("hello C from Visual Stido 2010\n");
         ptrNumber1= &number1;
         printf("%d\n",number1);
         printf("%d\n",*ptrNumber1);
         printf("%d\n",ptrNumber1);
         printf("%d\n",&number1);
         getch();
}


The code above is quiet simple:

  • Declaring a pointer variable
  • Declaring a pointer
  • Assigning integer variable to pointer
  • Printing values and address of integer variable

In this way you can work with C language programs in Visual Studio. I have yet to explore how to execute data structures programs like Stack, Link List etc. in Visual Studio. Allow me to explore that and expect further articles on that. I hope this post is useful. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all