-3

I'm trying to exit the console screen i.e close the screen what command can i use to achieve this.

void main()
{
    int n;
    printf("Please enter a number less than 5");
    scanf("%d", &n);
    if(n <= 5)
       printf("good");
     else
     {
        printf("You entered a number above so the program will exit");
        //here i need to call a function or use a command that will close
        // the console screen;
     }
}

Any help will be appreciated thanks

sam
  • 853
  • 3
  • 19
  • 50
  • 2
    The `main` function should return an `int` – Basile Starynkevitch Dec 27 '18 at 17:15
  • 3
    The correct definition of main is `int main(void)`... – Ruks Dec 27 '18 at 17:17
  • @Basile Starynkevitch thanks for your help, if i let the main function return int and i return 0; what i get it "press any key to exit..." but i don't what that i want to exit automatically, is there any workaround – sam Dec 27 '18 at 17:18
  • 2
    Your program as-is will automatically close, depending on how it was compiled and launched. The way you defined the main function has absolutely no impact on whether it closes or not. Try compiling in release mode instead of debug, or vice versa, as well as running the program in a console instead of launching it via an IDE. Just play around with those parameters and see what happens. – Random Davis Dec 27 '18 at 17:19
  • In my answer, I am explaining that your question don't make any practical sense – Basile Starynkevitch Dec 27 '18 at 17:30
  • 3
    "press any key to exit..." is not something your code is doing - how are you launching the program? Most likely the message is emitted by whatever "shell" you launched the program with - an IDE for example. Rest assured your program has terminated - the program that launched it is what is being "exited". Run it from its Windows explorer icon for example to see how your code on its own behaves - it will terminate and its window close before you see _any_ output. Not sure why void / int main would make a difference, but again that is behavior specific to your IDE not your code. – Clifford Dec 27 '18 at 18:05
  • 1
    On Windows, you would use `FreeConsole` to close the console: https://learn.microsoft.com/en-us/windows/console/closing-a-console – mnistic Dec 27 '18 at 18:46

3 Answers3

4

The C11 standard n1570 does not know about the "console screen" (and I guess you speak of the terminal emulator running your program). Notice that C11 does not mention "screens" or "keyboards" (only standard streams, and very often stdout is not a "console") and many computers (e.g. most Internet servers or supercomputers, or even your mobile phone...) don't have both. Also, your program could be run (even on Windows) with redirections or in a pipeline and then it has no console (so your question don't make any sense in such a common case).

So in general, there is no way to do what you want (since it does not make any sense), in a standard way.

Perhaps your operating system provide some (OS specific) way to achieve that. So investigate the OS API relevant to your system (e.g. WinAPI on Windows, or Linux syscalls -listed in syscalls(2)).

Perhaps you want to use some terminal related library like ncurses.

If your terminal follows the ANSI escape code conventions, you might follow them.

Otherwise, consider making your program having some GUI. For that, you practically need some widget toolkit (such as Qt, GTK, etc..)

You might also consider some inter-process communication with your desktop environment. How to do that (or even its possibility) is very operating-system and desktop specific and might be related to session management.

BTW, remember that stdout is often buffered (and perhaps line-buffered). You'll better end your printf control strings with \n and/or call fflush.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • yes i'm taking about the terminal and i'm on windows OS is there a way around this or it can't be achieve in anyway – sam Dec 27 '18 at 17:23
  • Even on Windows, you can have redirections (and that is very common). In that case your question does not make any sense. – Basile Starynkevitch Dec 27 '18 at 17:24
  • 1
    @sam : The fact that your are running on Windows is important information that should be in your question or at th every least tagged since Window management is platform specific. – Clifford Dec 27 '18 at 17:58
  • 1
    In Windows the term "console" is correct and ambiguous I think. A console mode program need not be launched from a terminal emulator (and In Windows the command shell itself does not run in a terminal emulator.either - it is itself a "console mode" program). If you were to run the code from a terminal or on Windows a cmd or powershell session, then the window is not the program's to close as the program is running as a child of the shell. It seems here the confusion is caused by the manner in which the program is executed - it does not own the parent window as it was not launched directly. – Clifford Dec 27 '18 at 18:16
3

In a windowing operating system or execution environment the console window will close immediately the process terminates, so it is not clear what you are asking here since in your example the program terminates regardless of what input is entered.

If you are running the code from an IDE, often the IDE will create a console process and launch your code within that. In that case the console is not "owned" by your application, but is executed as a child process; in which case the window will remain open until the parent process launched by the IDE is closed. Similarly if you launch your program from a command shell. It is probably unreasonable behaviour for a process to attempt to close its parent even if it is possible.

It is possible to "hide" the console window while the process continues to run, which may be what you are asking; the means of doing that is platform specific, and you have not specified; for Windows such a question would be a duplicate of Win32 programming hiding console window. However it is quite possible that these methods will not work if the process is not launched directly but from some other console process.

Clifford
  • 88,407
  • 13
  • 85
  • 165
0

in Windows you may simply write code on Notepad, then compile and run it through the Command prompt (cmd.exe). If you have GCC installed as compiler (with all the needed packages), then compile your main.c file as:

gcc main.c -o main.exe

If all went fine, as you run "main", there will be all of your output that you can close or step over for more editing. Bye

PS EDIT -- I see your point: when you launch your .exe from itself, the window closes without giving satisfaction of the messages. You may add a workaround like this before the last curly bracket:

    printf("Press any key\n");
    scanf("%d");

}

So the output window will still wait for one more input before closing.

You may check for additional information for eg here: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html

Bye

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 18 '22 at 17:46