943,864 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2929
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 28th, 2007
0

Stupid Question

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lost in Code... is offline Offline
22 posts
since Nov 2006
Aug 28th, 2007
0

Re: Stupid Question

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 2:23 pm.
Reputation Points: 85
Solved Threads: 13
Junior Poster
TkTkorrovi is offline Offline
170 posts
since Mar 2005
Aug 28th, 2007
0

Re: Stupid Question

Click to Expand / Collapse  Quote originally posted by TkTkorrovi ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lost in Code... is offline Offline
22 posts
since Nov 2006
Aug 28th, 2007
0

Re: Stupid Question

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 3:10 pm.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 28th, 2007
0

Re: Stupid Question

Click to Expand / Collapse  Quote originally posted by TkTkorrovi ...
  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 10:50 pm.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 29th, 2007
0

Re: Stupid Question

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.

Reputation Points: 10
Solved Threads: 0
Newbie Poster
Lost in Code... is offline Offline
22 posts
since Nov 2006
Aug 29th, 2007
0

Re: Stupid Question

Click to Expand / Collapse  Quote originally posted by SpS ...
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 9:29 am.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Aug 29th, 2007
0

Re: Stupid Question

Doesn't printing \n do the same thing as fflush?
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Aug 29th, 2007
0

Re: Stupid Question

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 12:43 pm.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 29th, 2007
0

Re: Stupid Question

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 5:07 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ali_smith_14 is offline Offline
1 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Dll
Next Thread in C Forum Timeline: Scanning (with TWAIN)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC