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

Recommended Answers

All 17 Replies

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.

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?

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.

printf ("== press enter ==\n");
getchar ();

This would be better

printf("Hit 'ENTER' to exit\n");
fflush(stdout);
(void)getchar();

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.

:)

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 ?

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

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.

you can use :

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

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

return 0;
}

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. :(

commented: good advice Hammie +11

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.

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.

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.

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.

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!

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.

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.