How to run a compiled file

How to run a compiled file

Post by alexphang88 on Sun Jan 09, 2011 3:27 am
([msg=51897]see How to run a compiled file[/msg])

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.
Last edited by alexphang88 on Sun Jan 09, 2011 5:52 am, edited 2 times in total.
alexphang88
New User
New User
 
Posts: 7
Joined: Mon Jan 04, 2010 11:09 am
Blog: View Blog (0)


Re: How to run a compiled file

Post by s0fa on Sun Jan 09, 2011 3:49 am
([msg=51898]see Re: How to run a compiled file[/msg])

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:
"The individual is defined only by his relationship to the world and to other individuals; he exists only by transcending himself, and his freedom can be achieved only through the freedom of others." - Simone de Beauvoir
s0fa
New User
New User
 
Posts: 31
Joined: Tue Jan 04, 2011 2:12 pm
Blog: View Blog (0)


Re: How to run a compiled file

Post by alexphang88 on Sun Jan 09, 2011 4:47 am
([msg=51901]see Re: How to run a compiled file[/msg])

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*
alexphang88
New User
New User
 
Posts: 7
Joined: Mon Jan 04, 2010 11:09 am
Blog: View Blog (0)


Re: How to run a compiled file

Post by s0fa on Sun Jan 09, 2011 5:12 am
([msg=51902]see Re: How to run a compiled file[/msg])

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.
"The individual is defined only by his relationship to the world and to other individuals; he exists only by transcending himself, and his freedom can be achieved only through the freedom of others." - Simone de Beauvoir
s0fa
New User
New User
 
Posts: 31
Joined: Tue Jan 04, 2011 2:12 pm
Blog: View Blog (0)


Re: How to run a compiled file

Post by fashizzlepop on Sun Jan 09, 2011 5:38 am
([msg=51903]see Re: How to run a compiled file[/msg])

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.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: How to run a compiled file

Post by s0fa on Sun Jan 09, 2011 5:39 am
([msg=51904]see Re: How to run a compiled file[/msg])

[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.
"The individual is defined only by his relationship to the world and to other individuals; he exists only by transcending himself, and his freedom can be achieved only through the freedom of others." - Simone de Beauvoir
s0fa
New User
New User
 
Posts: 31
Joined: Tue Jan 04, 2011 2:12 pm
Blog: View Blog (0)


Re: How to run a compiled file

Post by fashizzlepop on Sun Jan 09, 2011 5:45 am
([msg=51905]see Re: How to run a compiled file[/msg])

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.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: How to run a compiled file

Post by s0fa on Sun Jan 09, 2011 6:13 am
([msg=51909]see Re: How to run a compiled file[/msg])

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*
"The individual is defined only by his relationship to the world and to other individuals; he exists only by transcending himself, and his freedom can be achieved only through the freedom of others." - Simone de Beauvoir
s0fa
New User
New User
 
Posts: 31
Joined: Tue Jan 04, 2011 2:12 pm
Blog: View Blog (0)


Re: How to run a compiled file

Post by alexphang88 on Sun Jan 09, 2011 10:27 am
([msg=51913]see Re: How to run a compiled file[/msg])

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.
alexphang88
New User
New User
 
Posts: 7
Joined: Mon Jan 04, 2010 11:09 am
Blog: View Blog (0)


Re: How to run a compiled file

Post by insomaniacal on Sun Jan 09, 2011 12:13 pm
([msg=51920]see Re: How to run a compiled file[/msg])

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.
It's not who votes that counts, it's who counts the votes
insomaniacal.blog.com
User avatar
insomaniacal
Addict
Addict
 
Posts: 1210
Joined: Sun May 24, 2009 10:21 am
Blog: View Blog (0)


Next

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron