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

[QUOTE=TkTkorrovi;426260]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.[/QUOTE]It doesn't demand it, and it is rather easy to turn …

Jump to Post

All 17 Replies

Re: Stupid Question 80 80

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.

Re: Stupid Question 80 80

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?

Re: Stupid Question 80 80

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.

Re: Stupid Question 80 80
printf ("== press enter ==\n");
getchar ();

This would be better

printf("Hit 'ENTER' to exit\n");
fflush(stdout);
(void)getchar();
Re: Stupid Question 80 80

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.

:)

Re: Stupid Question 80 80

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 ?

Re: Stupid Question 80 80

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

Re: Stupid Question 80 80

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.

Re: Stupid Question 80 80

you can use :

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

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

return 0;
}

Re: Stupid Question 80 80
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
Re: Stupid Question 80 80

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.

Re: Stupid Question 80 80

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.

Re: Stupid Question 80 80

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.

Re: Stupid Question 80 80

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.

Re: Stupid Question 80 80

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!

Re: Stupid Question 80 80

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.

Re: Stupid Question 80 80

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.