I am a beginner of C. I run the C program, but the window closes too fast before I can see anything. How can I pause the window?
If you want to just delay the closing of the window without having to actually press a button (getchar()
method), you can simply use the sleep()
method; it takes the amount of seconds you want to sleep as an argument.
#include <unistd.h>
// your code here
sleep(3); // sleep for 3 seconds
References: sleep() manual
you can put
getchar();
before the return from the main function. That will wait for a character input before exiting the program.
Alternatively you could run your program from a command line and the output would be visible.
You could also just use system("pause");
Good job I remembered about DOS Batch files. Don't need Getchar() at all. Just write the batch file to change directory (cd) to the folder where the program resides. type the name of the exe program and on the next line type pause. example:
cd\
wallpaper_calculator.exe pause
getch()
can also be used which is defined in conio.h.
The sample program would look like this :
#include <stdio.h>
#include <conio.h>
int main()
{
//your code
getch();
return 0;
}
getch()
waits for any character input from the keyboard (not necessarily enter key).
I assume you are on Windows. Instead of trying to run your program by double clicking on it's icon or clicking a button in your IDE, open up a command prompt, cd to the directory your program is in, and run it by typing its name on the command line.