Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

clrscr in C

Last Updated on January 9, 2024 by Ankit Kochar

Clearing the console screen in C programming is a fundamental function utilized by developers to enhance the user experience and maintain a clean interface. Among the various methods available, clrscr stands out as a commonly used command to wipe the screen of any output or clutter, providing a fresh canvas for new information. In this article, we delve into the intricacies of clrscr in C, exploring its usage, implementation, and best practices.

clrscr in C

The clrscr in C is a built-in function that is used for clearing the screen of the console output during the execution of the C program. This function is defined in the conio.h header file. We need to include the “conio.h” file for using the clrscr in C. This function is useful in the case of the console-based program as it allows the programmer to clear the screen and starts displaying the output from the top of the screen.

Syntax of clrscr in C

The syntax of the clrscr in C is given below:

clrscr();

To use this function we need to include the stdio.h file by using the statement given below.

#include 

The clrscr() function is declared in the stdio.h header file, which is why it is necessary to include this header file in any program that uses this function.

Working of clrscr in C

The clrscr() function in C works by clearing the console screen (with any previous output that may have been present on the screen). It should be noted that this function does not delete any previously written data or any code from the program, it just simply clears the screen and allows the programmer in showing the new output from the top of the screen.

Examples of clrscr in C

Let us see some examples so that we can understand the concept of clrscr in C.

Example 1 of clrscr in C
The following is a simple example that shows how we can use the clrscr in C program.

Code

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("Hello World!");
    getch();
    clrscr();
    return 0;
}

Output:

Hello World!

Explanation:
The above C code is just printing the “Hello World!” on the console screen. We have used the printf() Function to display the output on the console. The getch() function is used to wait for the user to press a key before the screen is cleared. Then we used the “clrscr()” function which clears the screen and allows the programmer to display the next output from the top of the screen.

Example 2 of clrscr in C
Here is another example showing the usage of the clrscr in C.

Code (with clrscr Function in C):

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j, k;
    clrscr();

    printf("Enter the value of i: ");
    scanf("%d", &i);
    printf("Enter the value of j: ");
    scanf("%d", &j);
    k = i + j;
    clrscr();

    printf("The value of i is %d\n", i);
    printf("The value of j is %d\n", j);
    printf("The sum of i and j is %d\n", k);
    
    getch();
    clrscr();
    return 0;
}

Input:

5
8

Output:

The value of i is 5
The value of j is 8
The sum of i and j is 13

Explanation:
In the above code, we have used clrscr() in the starring to clear the output screen( it may contain the output of the previously run code). Next we have asked the user to enter the values of two variables i and j using the scanf() Function. Then we added the values of i and j and stored them in the variable k. Then again we used the clrscr Function to clear the screen and then we protected the output. This results in the output shown above.

Let us also see what happens if we don’t use the second clrscr() function.

Code (Without clrscr in C)

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i, j, k;
    clrscr();

    printf("Enter the value of i: ");
    scanf("%d", &i);
    printf("Enter the value of j: ");
    scanf("%d", &j);
    k = i + j;
    //clrscr();

    printf("The value of i is %d\n", i);
    printf("The value of j is %d\n", j);
    printf("The sum of i and j is %d\n", k);
    
    getch();
    clrscr();
    return 0;
}

Output:

Enter the value of i: 5
Enter the value of j: 8
The value of i is 5
The value of j is 8
The sum of i and j is 13

This is the output if we don’t use the clrscr() Function in the above code.

Note: The above codes run only on the older compiler (such as Turbo C) and give errors on the modern compilers as they do not include the “stdio.h” file”.

Conclusion
Understanding how to effectively utilize clrscr in C programming can streamline console-based applications by ensuring a neat and organized user interface. While it remains a legacy function with restricted applicability in modern programming, its usage elucidates fundamental concepts in managing console output and screen manipulation in C. As software development continues to evolve, clrscr retains its significance in certain niche areas within the programming landscape.

Frequently Asked Questions (FAQs) about clrscr in C

Here are some Frequently Asked Questions on “clrscr in C”.

1. Is clrscr recommended for modern C programming practices?
With modern graphical user interfaces (GUIs) dominating, the usage of clrscr is limited. It’s more commonly used in console-based programs or for educational purposes rather than in contemporary software development.

2. How is clrscr implemented in C?
The clrscr function is generally implemented using system-specific commands or library functions that clear the screen, such as system("cls") for Windows or system("clear") for Unix-based systems.

3. Does clrscr erase data or just clear the screen?
clrscr only clears the visible content on the screen, leaving the underlying data intact. It does not erase or modify any stored variables or data in the program.

4. Is clrscr part of the C standard library?
No, clrscr is not part of the C standard library. It is commonly found in certain compilers or header files like or .

5. Can clrscr be used in all C compilers?
clrscr may not be supported by all C compilers as it’s not a standardized function. Its availability depends on the compiler and the system for which the program is developed.

6. Are there alternative methods to clear the screen without using clrscr?
Yes, alternative methods include using escape sequences like printf("\033[H\033[J") or system-specific commands (system("cls") for Windows or system("clear") for Unix-based systems).

7. When should clrscr be used in a C program?
clrscr is typically used when a program needs a clean console output or when transitioning between different sections of the program requiring a clear screen for better user interaction.

8. Does using clrscr impact program performance?
The impact on performance is minimal for small-scale applications, but for larger programs, excessive use of clrscr might cause flickering and slower output due to screen clearing operations.

9. Can clrscr clear only a specific portion of the screen?
No, clrscr clears the entire console or terminal window, removing all content from the top-left corner to the bottom-right corner.

Leave a Reply

Your email address will not be published. Required fields are marked *