My compiler won't pause on getchar();. I have Dev C++ and if I put getchar(); at the end of the program, it doesn't stop, and the console window still exits. It still exits when I assign a variable to it
c = getchar();
Is there another way to pause the program with C, without using system("PAUSE");

Dani AI

Generated

Two things are happening here, and both showed up in this thread. First, as noted, if you read a number with scanf, the trailing newline stays in the input buffer. The very next character-read will grab that newline instantly, so it looks like your "pause" was ignored. Second, as said, the console window vanishing is usually your IDE, not C. If you double-click the EXE, Windows closes the console when the process ends. Run it from a Command Prompt so the window stays open.

If you want a reliable, portable pause without nonstandard calls, drain any pending input, then wait for a fresh Enter. This avoids the leftover-newline trap:

/* flush any pending characters (e.g., after scanf) */
int ch;
while ((ch = fgetc(stdin)) != '\n' && ch != EOF) {}

/* now pause for a brand-new Enter */
printf("Press Enter to exit...");
fflush(stdout);
while ((ch = fgetc(stdin)) != '\n' && ch != EOF) {}

Input strategy matters too. @iamthwee suggested reading as strings; that can be a good pattern if you pair it with proper parsing. A common, robust workflow is: read a whole line with fgets, then parse with strtol/strtod (or sscanf), and reject bad input. That keeps your stream state predictable and prevents surprise newlines from breaking later reads. It also aligns with ’s point about understanding how the stream works rather than swapping one mystery for another.

Quick cautions: avoid fflush(stdin) (undefined by the C standard), and remember _getch() is nonstandard and Windows-only. Finally, switching IDEs ( moved to Pelles C) can change default run behavior, but the underlying fix is to control how you launch the program and how you manage the input stream.

Recommended Answers

All 12 Replies

The program will not stop if there are still keystroks in the keyboard buffer, such as if you previously called scanf() to get an integer the '\n' will still be in the keyboard buffer. But you will have to post your program for anyone to give you more help with this.

>Is there another way to pause the program with C, without using system("PAUSE");
Sure. Don't run your program in an IDE that isn't smart enough to keep the window open. Run it from the command prompt.

Member Avatar for Member #46692

The problem arises when you are using different variables to read stuff, like ints and strings.

My advice would be to read everything in as a string and just convert to ints or floats where necessary.

The problem arises when you don't know how C uses stream based input. You can mix input methods without any trouble if you know what happens, but most people just make function calls without knowing what's going on. The ideal fix is to learn how stream I/O works and learn what each input function does.

Member Avatar for Member #46692

That too but my way is best, safer (for newbies ) and works :)

Its ok, I switched to a new IDE and it solved the problem. Thanks for the help guys.

>but my way is best
No, your way promotes ignorance. Mine promotes learning.

>safer (for newbies )
Trading one mystery for another isn't safer.

>and works
Which is why I didn't immediately correct you.

Member Avatar for Member #46692

LOL

Which IDE did you change too. BTW I think you should try my idea cos I bet it works.

Pelles C. Its a good IDE, similar to Dev C++. I don't know how to convert from type to another anyways...

Member Avatar for Member #46692

You also know if you run your program from the command line you can see the output.

i.e start > run > cmd cd "path to exe" blah.exe ? If I recall.

Try to use _getch();

>Try to use _getch();
Bumping a three year old thread with bad advice. Awesome first post! :icon_rolleyes:

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.