Preventing the console window from closing when writing a console program in C. C / C Forums on Bytes. Oct 02, 2017 In this video, I show you how to pause a C console program, so you can keep that window open! The answer we cover is to use a C library function: cin.get.
![]()
Dev-C++ v 4.9.9.2 IDE
When I compile and run my program as a console project, a window flashes very briefly on the screen and disappears. The compile log says compilation was successful and execution terminated.
How to I keep the window (my output window?) from disappearing?
Ancient Dragon5,243
You have to add a line just before the end of main() to stop the program from closing. Most people call getch() or c++ cin.get(), which is just waiting for keyboard entry.
The things that a C program can do are limitless, but when you’re first learning the language, you need to start small. One of the most common functions you’ll want your C program to do is display text on the screen, and there are two ways to do so: puts() and printf().
puts()
Puts probably stands for put string, where a string is a bit of text you put to the screen. Regardless, here’s how it works:
The text to display — the string — is enclosed in the function’s parentheses. Furthermore, it’s enclosed in double quotes, which is how you officially create text inside the C language, and how the compiler tells the difference between text and programming statements. Finally, the statement ends in a semicolon.
Here’s how puts() might fit into some simple source code:
The puts() function works inside the main() function. It’s run first, displaying the text Greetings, human! on the screen. Then the return(0); statement is run next, which quits the program and returns control to the operating system.
printf()
Another C language function that displays text on the screen is printf(), which is far more powerful than puts() and is used more often. While the puts() function merely displays text on the screen, the printf() function displays formatted text. This gives you more control over the output.
Try the following source code: How to get the message box back in dev c++.
Type this code into your editor and save it to disk as HELLO.C. Then compile it and run it.
You probably assumed that by putting two printf() statements on separate lines, two different lines of text would be displayed. Wrong!
The puts() function automatically appends a newline character at the end of any text it displays; the printf() function does not. Instead, you must manually insert the newline character (n) into your text.
To “fix” the line breaks in the preceding HELLO.C file, change line 5 as follows:
How To Pause The Output Screen In Dev C Pdf
The escape sequence n is added after the period. It’s before the final quotation marks because the newline character needs to be part of the string that’s displayed.
How To Pause The Output Screen In Dev C Windows 7
So save the change, recompile HELLO.C, and run it. Now the output is formatted to your liking:
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |