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.

 

44 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….

  2. Good one Dhananjay…
    same we can do in VS 2008

  3. its very useful for me to learn c in VS 2010

  4. really awesome post , you took me to my old school days

  5. […] How to write and run a C Program in Visual Studio 2010 […]

  6. Thank you sir 🙂
    Derek

  7. very good post brother…….

  8. thank you sir

  9. Thanks .
    You have done an awesome job.

  10. Many thanks, Sir,
    I hope we can more see about the good old C – school here.

  11. Dhananjay Kumar

    I will do

    /DJ

  12. A very good lesson given Thnx

  13. Visual Studio can’t seem to debug all codes. Can someone try debugging the codes below? It works for cygwin.

    #include
    /* count digits, white space, others */
    main()
    {
    int c, i, nwhite, nother;
    int ndigit[10];
    nwhite = nother = 0;
    for (i = 0; i = ‘0’ && c <= '9')
    ++ndigit[c-'0'];
    else if (c == ' ' || c == '\n' || c == '\t')
    ++nwhite;
    else
    ++nother;
    }
    printf("digits =");
    for (i = 0; i < 10; ++i)
    printf(" %d", ndigit[i]);
    printf(", white space = %d, other = %d\n",
    nwhite , nother);
    getchar();
    }

  14. Thanx a ton !! Frankly speaking I’m new to Microsoft Visual Studio 2010 and I’m dealing with a software which is made in C. And with your help i was able to find C in MVS. 🙂 .. But can you help me more as I’m also not so sound in C programming? Will you!!

  15. VS screen can not remain constant at the time of out put given by the program
    suggest solution

  16. when I run the programme, It said it’s out of date……….. what can I do dear?

  17. Thank you so much!

  18. Thanxx a ton for this….:-)

  19. Sandeep Kumar Das

    when I run the following program
    #include
    #include
    void main()
    {
    printf(“my name is sandeep”);
    getch();
    }
    It shows two error as follows
    Warning 1 warning C4627: ‘#include ‘: skipped when looking for precompiled header use c:\users\sandeep_kumar_das\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\source.cpp 2 1 ConsoleApplication5
    Error 2 error C1010: unexpected end of file while looking for precompiled header. Did you forget to add ‘#include “stdafx.h”‘ to your source? c:\users\sandeep_kumar_das\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\source.cpp 8 1 ConsoleApplication5

    please any body help me

  20. please tell me how can i call a c program to operate on the data taken in windows form text box in visual studio ?? i want to call a c program with some arguments from a form..

  21. WellDone….. good work

  22. Dhananjay Kumar

    Thanks 🙂

  23. I was really looking for this… but was getting always different.

    Thanks a lot 🙂

  24. Thanks a lot. Kenya is reading you!

  25. Thanks !

  26. A lot of thanks!!!!!!!!!!!! 🙂

  27. i am having problem – none of my c codes are running . they are showing error

  28. Good simple steps

  29. How to write Customer Information like: ID, Name, Account number, Phone number Address and run a C Program in Visual Studio 2010

  30. after after compilngt its showing error that its unable to execute c/user etc path namey

  31. Hello ! i need a c code to search a word in a file

  32. There is a problem with linked list null pointer handling in Visual studio , how to overcome it..

  33. thnks buddy …..
    now i am able to run my c prog on visual studio
    again thnks

  34. pawanvir singh

    thanks dhananjay ji

  35. nice thanks

  36. i am using VS, but when i debug it, if output is kind of printing of line output window stays for long, but if i wanted to print any value as output then it compiles, shoes no error then its output window, just flashes and disappears within second, so can’t observe output value, how to fix it??, what will be solution for it?

  37. Getting problems…
    Error 3 error LNK1123: failure during conversion to COFF: file invalid or corrupt C:\Users\DEEP\Documents\Visual Studio 2010\Projects\deep\deep\LINK deep

  38. Thank you so much! This helped me so much!

  39. This exactly what I’ve been seeking for many days! It’s clear and useful!

    Thanks.

  40. […] How to write and run a C Program in Visual Studio 2010 … – 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 … […]

  41. […] How to write and run a C Program in Visual Studio 2010 … – 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 … […]

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

Create a website or blog at WordPress.com