I'm trying to learn C programming, but when I run my code the cmd, window closes immediately, without giving me the change to see if the program printed the result I was aiming for.
I'm coding C on VS-Code, using several extensions. Is there a setting/extension/code snippet, or anything I can do so it won't close immediately?
Thanks!
You can simply press Ctrl + F5 instead of F5 to run the built code. Then it will prompt you to press any key to continue. Or you can use this line -> system("pause"); at the end of the code to make it wait until you press any key.
Keep Console Open With the Ctrl + F5 Shortcut in C# The best approach for keeping our console window open after the execution of code is to run it with the Ctrl + F5 shortcut of the Microsoft Visual Studio IDE.
Before the end of your code, insert this line: system("pause"); This will keep the console until you hit a key.
Start the project with Ctrl + F5 instead of just F5 . The console window will now stay open with the Press any key to continue . . . message after the program exits.
The easiest (and most common) way to do this is to add the line system("pause");
immediately before the return 0;
statement in your main
function:
#include <stdio.h>
#include <stdlib.h> // This header defines the "system()" function
// For C++ builds, #include <iostream> will suffice
int main()
{
printf("Hello, World!\n");
system("pause");
return 0;
}
This call will produce a prompt and await a key-press from the user. The exact message displayed may vary between compilers and/or platforms but, with Visual Studio and MSVC
, the message is:
Press any key to continue . . .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With