Yes, this is the expected one, after all, it does not have a loop holding or pause to avoid, the program runs what has to run and delivers main()
so the process ends and the window closes, this is the Exactly expected effect, it's no problem with the compiler, but with your understanding.
Program with graphical interface does not close because it has an internal (+ or -) loop, which prevents the process from ending while the window is open.
If you want to prevent the window from closing, just use pause
(in windows I think)
#include <stdio.h> /* printf */
#include <stdlib.h> /* system */
void main(){
printf("Teste");
system("pause");
}
Or an example you can do is to drag your executable after compiled into the cmd
window, press Enter on the cmd
screen the answer will appear:
getch
,getche
andgetchar
Waittotypesomethingtofinish:
#include<stdio.h>/*printf*/#include<conio.h>/*getch*/intmain(){printf("teste");
printf("\nPressione qualquer tecla para finalizar");
getch();
return 0;
}
Wait to type something and send what was typed into the output:
#include <stdio.h> /* printf */
#include <conio.h> /* getch e getche */
int main(){
printf("teste");
printf("\nDigite uma letra ou numero:\n");
getche();
printf("\nDigite qualquer coisa para finalizar");
getch();
return 0;
}
However, depending on this link , the use of anything related to conio.h
may not be reliable , then use getchar
:
#include <stdio.h>
int main ()
{
printf("Digite algo:");
getchar();
return 0;
}
If you need to get the output:
#include <stdio.h> /* printf e getchar */
int main (){
printf("Digite algo:");
int test = getchar();
putchar(test);
return 0;
}