Page 1 of 2

How to run a compiled file

PostPosted: Sun Jan 09, 2011 3:27 am
by alexphang88
Hi all, I am trying to start learning C and I have no prior knowledge on programming what so ever.

I downloaded the Dev-C++ software and started to write the codes in it, saved it as a .c file, compiled it into a .exe file then ran the .exe file. But when I run the .exe file I just see some black window open and closes really fast, and I don't see the anything like what the book showed, a black window with words in it.

The code is

Code: Select all
#include <stdio.h>
main()
{
printf (“Howdy, neighbor! This is my first C program.\n”);
return 0;
}


Straight from the book. It is supposed to display the msg “Howdy, neighbor! This is my first C program.\n” but the situation as described above happened instead. May I ask what is wrong?

Btw, I run on Windows 7 32 bit.

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 3:49 am
by s0fa
That has nothing to do with your operating system, it's an absolutely normal behaviour.
You tell the computer that he has to print "Howdy, neighbor! This is my first C program." now. (That's the text in the black windows, the command shell.) After doing that the computer "thinks":"Yeah, nice. Going back to normal now!" and he's closing the window. Try to add the command "system("PAUSE") which will make the commando prompt stay open until you push a button.

Code: Select all
#include <stdio.h>
main()
{
printf (“Howdy, neighbor! This is my first C program.\n”);
return 0;
system("PAUSE")
}


Later on, you should avoid this function, but for the moment it's okay - afterwards, just forget it. :mrgreen:

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 4:47 am
by alexphang88
So, suppose I want to make something like a calculator with it, do I still need to include the system("PAUSE") command there?

And from the looks of it, there are other system("something") functions?

*Btw, thanks for the help, I will try it once I get home*

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 5:12 am
by s0fa
Your terminal (I'm sorry for that unix-term) will stay open as long as there is something to do has something to do, so for example it made all his calculations and displays you the result - if there is nothing after that result (Maybe a dialogue if u want to restart to program or something like that) the terminal will close.

And from the looks of it, there are other system("something") functions?

I'm not very much into C++, to be honest, but there are some other "system specific" things such as special libraries or the Windows API and so on - but that does not affect you in the beginning. Functions in the style of system("something") are not known to me, but I give you absolutely no guarantee that there is really nothing like that.

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 5:38 am
by fashizzlepop
alexphang88 wrote:So, suppose I want to make something like a calculator with it, do I still need to include the system("PAUSE") command there?<br>

Don't use that command. You can simply use "getchar();" at the end. If you make a calculator, you will want to add it at the end, use it as a "Press ENTER to exit."

Also, you might as well just run it from cmd. cd to the directory and run your executable. It will be displayed and leave you with the command prompt again.
<br>And from the looks of it, there are other system("something") functions?<br><br>*Btw, thanks for the help, I will try it once I get home*

"system()" just passes the input to the command prompt I'm pretty sure.

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 5:39 am
by s0fa
[quote="fashizzlepop"]Don't use that command. You can simply use "getchar();" at the end. If you make a calculator, you will want to add it at the end, use it as a "Press ENTER to exit."
Also, you might as well just run it from cmd. cd to the directory and run your executable. It will be displayed and leave you with the command prompt again.[quote]
cin.get would also be a possible solution for him. Oh, and starting it out of the command prompt is sometimes difficult while using Windows.

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 5:45 am
by fashizzlepop
cin.get is more C++ isn't it?

And its not hard at all in cmd... I'm not sure why you would think so.

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 6:13 am
by s0fa
fashizzlepop wrote:cin.get is more C++ isn't it?
And its not hard at all in cmd... I'm not sure why you would think so.

Ah, oh my gosh I'm getting confused - you are absolutely right!
Concerning the cmd, maybe I was just too stupid to do so, it's easier on Linux. *grin*

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 10:27 am
by alexphang88
Hmm . . . is it a compiler problem? The codes in the example in the book has no added codes in them, yet they seem to be able to be displayed in the console (or whatever the name was, the black window thing), the example uses Borland.

Re: How to run a compiled file

PostPosted: Sun Jan 09, 2011 12:13 pm
by insomaniacal
They probably opened up a console, and ran it from there. For example, if your program is C:\a.exe, they would open up cmd an d type C:\a.exe in order to execute it. It would print what you tell it to and still keep the window open.