How to write and run a C Program in Visual Studio 2010
Join the DZone community and get the full member experience.
Join For Freetoday 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,
from the dialog box click on next button
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 .
after clicking finish you will find a project has been created with below structure. open solution explorer to see the structure
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
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,
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.
Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments