I run the simplesest c hello world function in codelite just to see it works well
void main ( )
{
printf("hello world\n");
}
the cmd promt opens for q brief momet and then closes down immediatly.
what's wrong and how can I keep the cmd prompt without closing?
I excepct for the cmd prompt to stay
CodePudding user response:
From C11:
If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
After the call to printf
; control reaches the closing brace }
, terminates main
, and thus returns to the host environment with an exit status of EXIT_SUCCESS
, or 0
.
Also note that it states:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main (void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main (int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
CodePudding user response:
You did not mention your OS, so I will assume Windows. Like other people mentioned, unless you add a call to getchr() or something similar, this is the expected behavior.
However, CodeLite does offer an option to pause your program once the execution is done.
- Right click on your project and open the settings
- Under the
General
tab, in theExecution
section, make sure to check the option:Pause when execution ends
(see attached screenshot)
What this does: Instead of running your program directly, CodeLite executes a small wrapper that will execute your program, and that wrapper pauses when its done.
HTH, Eran