Ömer Çiftci
Ömer Çiftci

Reputation: 41

I can't keep open console in C

I started to learn C and I am using Visual Studio for this. But I can't keep open console when I compile it. I wasn't do anything others program like Dev-C++ or Codeblocks. How can I solve it

#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("My name is Alex\n");
return 0 ;
}

Upvotes: 1

Views: 312

Answers (3)

paxdiablo
paxdiablo

Reputation: 881113

If you want VS to keep your console window open, you can modify your code to put getchar() before main() exits, but that will only work if your code actually exits via that method.

Having it crash or call exit() or any one of a dozen other ways that bypasses exiting via main() will not work with that method.

In any case, VS itself will do this for you in a much cleaner way. Simply use CTRL-F5 (start without debugging) instead of F5 (start debugging) to run your code.

Doing so will cause VS to output the line:

Press any key to continue . . .

and await a keystroke before closing the console.

Upvotes: 1

Sachith Wickramaarachchi
Sachith Wickramaarachchi

Reputation: 5862

Use getch(); to keep console,like following

int main()
{
printf("My name is Alex\n");
getchar();
return 0 ;

}

Use code blocks IDE to practice,it will be helped to you,good luck :)

Upvotes: 1

Ahmar
Ahmar

Reputation: 750

It is strange that Visual Studio closes the console because it should not do that. You can explicitly provide a system("pause"); or getchar(); code-line at the end but before the return 0; line.

Upvotes: 1

Related Questions