command prompt instantly closing

Asked

Viewed 321 times

0

I am programming in C and using Code Runner to run the code, but whenever I click Run, the command prompt opens and closes very quickly. The problem is with the command prompt closing alone, because when I give a command to receive a value the prompt expects me to type something.

  • By any chance is he running the program and closing the screen? If you put to the end a reading instruction of type: printf("Tecle algo para encerrar: "); gets(x);.

1 answer

2


This is normal and even expected.

The prompt is open while your program is running, at the end it closes.


You can create forms of it not close immediately, it is quite common to use the function getchar() as a test, before the return of main, so the program awaits the key ENTER no pressure to close:

#include <stdio.h>

int main(void) {
  printf("Hello world\n");
  getchar();
  return 0;
}

With this, the data will be displayed at the prompt, but the function getchar going hold on the execution.

See online: https://repl.it/repls/MedicalCriticalExperiments


You may end up finding examples that use system("pause"), this will also hold running the program, but it will only work on Windows.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.