954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Stupid Question

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:

#include <stdio.h>

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

    return 0;
}


Thanks

Lost in Code...
Newbie Poster
22 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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:

printf ("== press enter ==\n");
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.

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 

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:

printf ("== press enter ==\n");
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?

Lost in Code...
Newbie Poster
22 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 
printf ("== press enter ==\n");
getchar ();


This would be better

printf("Hit 'ENTER' to exit\n");
fflush(stdout);
(void)getchar();
SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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.

:)

Lost in Code...
Newbie Poster
22 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This would be better

printf("Hit 'ENTER' to exit\n");
fflush(stdout);
(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 ?

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

Doesn't printing \n do the same thing as fflush?

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 
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.

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

you can use :

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

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

return 0;
}

ali_smith_14
Newbie Poster
1 post since Aug 2007
Reputation Points: 10
Solved Threads: 1
 
system("pause"); //stdio.h


system is in stdlib.h, not stdio.h. It's not a good idea either because now you depend on an outside program that might be malicious. It's a big security no-no because you created a hole that hackers can use to break into your program. :(

Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 
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.


lint is outdated, compilers have now good enough error checking, so that we don't need lint. gcc at least doesn't give such warning even when all warnings are on, i don't know any compiler which gives such warning, though maybe there is some.

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 
lint is outdated, compilers have now good enough error checking, so that we don't need lint.

Apparently you must be speaking from ignorance of the capabilities of a modern linter.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
Apparently you must be speaking from ignorance of the capabilities of a modern linter.


I likely don't know, what you call the "modern linter", but does it really demand (void) before some function calls? When the function's return value was not used, this means that we didn't need it, no need for such warnings.

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 
I likely don't know, what you call the "modern linter", but does it really demand (void) before some function calls? When the function's return value was not used, this means that we didn't need it, no need for such warnings.

It doesn't demand it, and it is rather easy to turn off that particular warning.

My frame of reference is PC-lint. This is not an ad, but some features it lists -- some of which I have used in the past -- are as follows. Strong Type Checking
Value Tracking
Checks on Weak Definials
Possibly Uninitialized
Function Mimicry
User-defined Function Semantics Checking

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

you can use :

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

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

return 0; }

I found out what the deal was.... instead of creating a new source file, i created a new project. When creating a new file, my tool writes this in the file as part of the template:

system("pause");


All good information though, thanks!

Lost in Code...
Newbie Poster
22 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
system("pause")

It works but it is platform dependant and incurrs a lot more overhead than getchar(). Most C/C++ developers of any ilk will tell you not to use system("pause").

If you search on this site or using Google you can find plenty of lengthy explanations why it's not a good practice.

hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
 

I found out what the deal was.... instead of creating a new source file, i created a new project. When creating a new file, my tool writes this in the file as part of the template:

system("pause");


Edit the template to get rid of that awful command. I did... :icon_wink:

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You