Stupid Question

Thread Solved

Join Date: Nov 2006
Posts: 22
Reputation: Lost in Code... is an unknown quantity at this point 
Solved Threads: 0
Lost in Code... Lost in Code... is offline Offline
Newbie Poster

Stupid Question

 
0
  #1
Aug 28th, 2007
I start C tomorrow and reading my book. When I run and compile this code it comes up in DOS and runs the code but then the DOS window closes. I'm new with C, can someone tell me why the DOS window flashes and then closes? Here's my code:
  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5. printf( "\t\"whoops\b\b\b\bew\n\"\n");
  6.  
  7. return 0;
  8. }

Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 170
Reputation: TkTkorrovi is on a distinguished road 
Solved Threads: 11
TkTkorrovi's Avatar
TkTkorrovi TkTkorrovi is offline Offline
Junior Poster

Re: Stupid Question

 
0
  #2
Aug 28th, 2007
It's for some reasons better to compile and run the programs using command line, but if you want to use ide, then you may write something like this in the end:
  1. printf ("== press enter ==\n");
  2. getchar ();
Notice that you cannot use any other single character than newline for that, when you use only standard c, because in standard c the standard input is line buffered.
Last edited by TkTkorrovi; Aug 28th, 2007 at 1:23 pm.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: Lost in Code... is an unknown quantity at this point 
Solved Threads: 0
Lost in Code... Lost in Code... is offline Offline
Newbie Poster

Re: Stupid Question

 
0
  #3
Aug 28th, 2007
Originally Posted by TkTkorrovi View Post
It's for some reasons better to compile and run the programs using command line, but if you want to use ide, then you may write something like this in the end:
  1. printf ("== press enter ==\n");
  2. getchar ();
Notice that you cannot use any other single character than newline for that, when you use only standard c, because in standard c the standard input is line buffered.
That seemed to do the trick, I'm a bit confused on the getchar() is it keeping the DOS window up because it's getting the charactor?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Stupid Question

 
0
  #4
Aug 28th, 2007
The window gets destroyed when the program ends. If you call getchar so that it blocks for input, the program doesn't end and the window isn't destroyed until getchar returns.
Last edited by Hamrick; Aug 28th, 2007 at 2:10 pm.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Stupid Question

 
0
  #5
Aug 28th, 2007
Originally Posted by TkTkorrovi View Post
  1. printf ("== press enter ==\n");
  2. getchar ();
This would be better
  1. printf("Hit 'ENTER' to exit\n");
  2. fflush(stdout);
  3. (void)getchar();
Last edited by SpS; Aug 28th, 2007 at 9:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 22
Reputation: Lost in Code... is an unknown quantity at this point 
Solved Threads: 0
Lost in Code... Lost in Code... is offline Offline
Newbie Poster

Re: Stupid Question

 
0
  #6
Aug 29th, 2007
So getchar is a function that's basically waiting for a charactor, thus keeping the DOS window open, correct?

I just want to make sure i'm understanding this correctly, thanks for your patients.

Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Stupid Question

 
0
  #7
Aug 29th, 2007
Originally Posted by SpS View Post
This would be better
  1. printf("Hit 'ENTER' to exit\n");
  2. fflush(stdout);
  3. (void)getchar();
I'd like to know more about why this is better. I understand the fflush to force the buffered printf output to the screen immediately. But why the cast to void for getchar's return value? Does this prevent allocation of bytes for the int ?
Last edited by hollystyles; Aug 29th, 2007 at 8:29 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Stupid Question

 
0
  #8
Aug 29th, 2007
Doesn't printing \n do the same thing as fflush?
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Stupid Question

 
0
  #9
Aug 29th, 2007
Originally Posted by hollystyles View Post
But why the cast to void for getchar's return value? Does this prevent allocation of bytes for the int ?
Since some compilers (and lint) will warn about discarded return values, an explicit cast to (void) is a way of saying "Yes, I've decided to ignore the return value from this call.
Last edited by SpS; Aug 29th, 2007 at 11:43 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1
Reputation: ali_smith_14 is an unknown quantity at this point 
Solved Threads: 1
ali_smith_14 ali_smith_14 is offline Offline
Newbie Poster

Re: Stupid Question

 
0
  #10
Aug 29th, 2007
you can use :

int main( void )
{
printf( "\t\"whoops\b\b\b\bew\n\"\n");

system("pause"); //stdio.h

return 0;
}
Last edited by ali_smith_14; Aug 29th, 2007 at 4:07 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 2439 | Replies: 17
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC