Open In App

C Program to Hide a Console Window On Startup

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Here in this article, we have created a console application that will write into a file. This console application will work in the background by hiding the console window. To ensure that the program or the console application is still running, we can see the live data appended in the text file created by the program.

Console Window

The console is literally “a window”  to the world of the computer.  It works on the concept of a text-only interface. The programs that run via the console are called the Console Application. Consoles are often called command Line interfaces, Command Prompts, or Command Terminals.

Console Application vs Window Application

The Console application and the Windows application are very similar to each other. Both are attached to a Console at some point in their lifespan. The Console application does not return the console until specifically mentioned but the Windows application immediately returns and frees the command prompt or the console.

Example: Here, we have created a C program that will create a text file and write some text to it. Program execution will take twenty seconds as it will sleep for two seconds for each write operation on the file.

C




// C Program to implement
// Hide console window
#include <stdio.h>
#include <windows.h>
  
int main()
{
    // object of fstream
    FILE* f;
    int i = 1;
  
    // loop to run this 10 times
    while (i < 10) {
        f = fopen("testing.txt", "a");
  
        // check if file is created.
        if (!f) {
            printf("File not created!");
        }
        else {
  
            // output on the Console screen.
            printf(
                "%dSuccessful creation of the file named "
                "as testing.txt\n",
                i);
  
            // write in the file
            fprintf(f, "%dHELLO FROM GEEKSFORGEEKS\n", i);
            i++;
        }
        fclose(f);
  
        // sleep for 2 second.
        Sleep(2000);
    }
  
    return 0;
}


Output:

Text appended to file in C

Text appended to file in C

In the above program, Opening the text file multiple times within the timespan of 20 seconds will show you that the file is being updated, but the console window is also present side by side.

Hiding Console Window

Now if we need to hide the console window and still want to append the text to the file, we need to do some programming that will execute the commands to hide the console window. Here we will use the “window.h” header file. At first console window will hide then the program will write to the file and after the writing is over the console is shown again.

Example:

C




// C Program to implement
// Hide Console Window
#include <stdio.h>
#include <windows.h>
  
int main(void)
{
    HWND console;
  
    // to allocate the console.
    AllocConsole();
  
    // find the console window and link to console
    console = FindWindowA("consoleWindowClass", NULL);
  
    // use the ShowWindow function to show and hide the
    // console. instead of SW_HIDE, we can also use 0 in
    // arguments.
    ShowWindow(console, SW_HIDE);
    Sleep(5000);
  
    // object of fstream
    FILE* fp;
    int i = 1;
  
    // loop to run this 10 times
    while (i < 10) {
        fp = fopen("testing.txt", "a");
  
        // check if file is created.
        if (!fp) {
            printf("File not created!\n");
        }
        else {
  
            // output on the Console screen.
            printf(
                "%dSuccessful creation of the file named "
                "as testing.txt\n",
                i);
  
            // write in the file
            fprintf(fp, "%dHELLO FROM GEEKSFORGEEKS\n", i);
            i++;
        }
        fclose(fp);
  
        // sleep for 2 second.
        Sleep(2000);
    }
  
    // show the console
    ShowWindow(console, 1);
    return 0;
}


Output:

The console reappears at the end of the program.

The console reappears at the end of the program.

Condition 2: If you don’t even want the console after the execution of the program, then delete the “ShowWindow(console, 1);” from the last line of the program.

Output for the second condition:

Console completely hidden

Console completely hidden

Advantages of Hiding Console

  • Some programs do not need user interaction, so no need for a console window.
  • The program can run in the background, without interrupting the user.

Disadvantages of Hiding Console

  • Progress of the program will not be visible.
  • Console inputs will hang the program.
  • Need to kill the program from the task manager to close the console app if continuously running.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads