Miscellaneous

How to write and run a C Program in Visual Studio 2010

image

Today I was recalling good days of language C and got tempted to play around POINTERS, STRUCTURE, and DATA STRUCTURE etc. I started to write some C code and as soon as I started, the first hurdle came that where to write code? I was running on 64 bit Windows 7 machine with Visual Studio 2010. I binged and found much suggestion to download this and install that etc. However being fan and loyal user of Visual Studio, I was more desired to use 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,

image

From the dialog box click on Next button

image

Next screen is of Application Setting. You need to make sure

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

image

After clicking Finish you will find a project has been created with below structure. Open solution explorer to see the structure

image

To start programming, right click on Source Files and add a new item. You need to make sure below 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 source file name as Sample1.C

image

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 console windows as below,

image

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 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();
}


Above code 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 am 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 blog posts on the same. I hope this post is useful. Thanks for reading.

 

About Dhananjay Kumar

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Discussion

7 Responses to “How to write and run a C Program in Visual Studio 2010”

  1. A complete step-by-step guide to write first program in C using Visual Studio….
    Old wine in a new bottle….

    Posted by Suvendu | February 7, 2012, 9:09 am
  2. Good one Dhananjay…
    same we can do in VS 2008

    Posted by Kirti Darji | February 7, 2012, 4:58 pm
  3. good job my 5

    Posted by jojo | February 9, 2012, 10:53 pm
  4. its very useful for me to learn c in VS 2010

    Posted by Manojkumar.k | February 13, 2012, 9:41 am
  5. really awesome post , you took me to my old school days

    Posted by Mushtaq Ilyas | February 21, 2012, 1:02 pm
  6. Thank you sir :-)
    Derek

    Posted by Derek | April 24, 2012, 9:42 pm

Trackbacks/Pingbacks

  1. Pingback: Monthly Report February 2012: Total Posts 19 « debug mode…… - March 3, 2012

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,380 other followers

Tweets

Categories

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2012