How to prevent the quick disappearance of window

K

kcwcuttingedge

I am just beginning to learn C program and I am using Bloodshed Dev-C++
5 (currently beta) on Window XP. When I was running a very simple
program:

#include
<stdio.h>

int main()
{
printf( "I am alive! Beware.\n" );
return 0;
}

the window displaying this message just disappear before I have any
time to see my output, the same happens for other program. Can anyone
tell me how to fix this problem?
 
S

spibou

I am just beginning to learn C program and I am using Bloodshed Dev-C++
5 (currently beta) on Window XP. When I was running a very simple
program:

#include
<stdio.h>

int main()
{
printf( "I am alive! Beware.\n" );
return 0;
}

the window displaying this message just disappear before I have any
time to see my output, the same happens for other program. Can anyone
tell me how to fix this problem?

If you ask in a Windows specific newsgroup I'm sure that many people
will
be able to give you an answer. Or read the documentation for this
Bloodshed
thing. Unfortunately all that is out of topic here.
 
R

Richard Bos

I am just beginning to learn C program and I am using Bloodshed Dev-C++
5 (currently beta) on Window XP. When I was running a very simple
program:

#include
<stdio.h>

This is not a valid preprocessor statement.
int main()
{
printf( "I am alive! Beware.\n" );
return 0;
}

the window displaying this message just disappear before I have any
time to see my output, the same happens for other program. Can anyone
tell me how to fix this problem?

Yes. Add a getchar() just before the return from main(). Or run it
directly from the command prompt, or use some Windows-specific function
to pause the program, but the getchar() call is both simple and ISO C.

Richard
 
J

Jordan Abel

Richard Bos said:
Yes. Add a getchar() just before the return from main(). Or run it
directly from the command prompt, or use some Windows-specific function
to pause the program, but the getchar() call is both simple and ISO C.

OT: one such appropriate windows-specific function is system("pause").

or you could just do
puts("");
puts("Press enter to continue...");
getchar();
return 0;
 
R

Robert Gamble

Jordan said:
OT: one such appropriate windows-specific function is system("pause").

or you could just do
puts("");
puts("Press enter to continue...");
getchar();
return 0;

It's worth noting that simply calling getchar() will leave all
characters except the first one on the input stream if the user entered
any before pressing the "return" key which may not be desirable. In
such a case something like "while(getchar() != '\n')" may be preferred.

Robert Gamble
 
A

av

It's worth noting that simply calling getchar() will leave all
characters except the first one on the input stream if the user entered
any before pressing the "return" key which may not be desirable. In
such a case something like "while(getchar() != '\n')" may be preferred.

Robert Gamble

int c;

while( (c=getchar())!= '\n' && c!=EOF);
 
K

Keith Thompson

Robert Gamble said:
It's worth noting that simply calling getchar() will leave all
characters except the first one on the input stream if the user entered
any before pressing the "return" key which may not be desirable. In
such a case something like "while(getchar() != '\n')" may be preferred.

This won't matter if it's the last thing you do before terminating the
program, but it's (almost) always a good idea to write code that could
be incorporated into something bigger later on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top