Question:
Microsft Visual Studio: How do i create a simple hello.c program in?
sezak
2009-09-21 19:04:07 UTC
All i just want to do is create hello.c file that can compile and run and print "hello" in the console.
#include

int main(){

printf("hello");
return 0;
}

I got it to compile but i can't get it to run.

Compiling...
assign1.cpp
Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Comp2003F09\assign1a\assign1a\Debug\BuildLog.htm"
assign1a - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

How do i get it to run?.
I tried Test -> Test in Current Context
and Text -> All Tests in Solution

But nothing happens.
Five answers:
balk
2009-09-21 19:23:21 UTC
EDIT:

Oh wait, I just noticed something. There is nothing in MSV Studio's output that shows that the program was LINKED.



Creating a Windows program is done in 2 steps. The first step is to compile the .cpp file into an .obj (object) file, then you need to LINK the .obj file with library code to build an .exe file. You shouldn't have to specify the library files to link with the .obj file when you make programs with the standard C or C++ libraries (it's usually set up for you in the IDE's Linker settings).

So, to create an .exe program, you have to find the menu item that says something like 'Build Solution' or something like that. Building the solution (i.e. your project) will compile the .cpp source file and then run the linker on the newly created .obj file to create the .exe file.

You probably just clicked on 'Compile' instead.



-----------

Uh, isn't there a 'Start Without Debugging' menu item in the 'Debug' drop-down menu? (It is there for the MS Visual C++ Express edition.)



If there isn't, then there has to be some menu item somewhere that let's you execute the program. Another way to run the program is to simply use Explorer to go to the folder where you built the .exe file and simply double-click the file.



By the way, when you test console programs, you need to pause the program at the end of the program because the console window will immediately close after the program ends. Usually, you just insert code that waits for a keypress.



You can insert this code before the 'return 0;' line:

while (!_kbhit()) {}



The _kbhit() function is defined in the CONIO.H header file, so include this line above or below the "#include " line:

#include
Marie
2016-05-21 05:03:33 UTC
Yes. I personally use Microsoft Visual Studio 2010 for coding in C++ on Windows 7 on my laptop. You can also use some alternative IDE's: 1- Dev-C++ 2- Bloodshed 3- Code blocks etc
GZ
2009-09-21 19:21:08 UTC
There's a little green arrow next to a drop-down menu that should say Debug under most circumstances. Press that to compile the program and run it at the same time.
jackthesetter
2009-09-21 19:22:43 UTC
If the compiling process went well, then there should be a hello.exe file in the bin directory. Check your location above. Open up a dos shell and run hello.exe.



If you run the application from VS, the application will run so quickly that you won't see it.
murphy
2009-09-21 19:36:41 UTC
Does it execute via the Menu Build->Execute "program" ? Ctrl+F5


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...