clrscr in C: Methods & its Examples

Published: 11 Feb 2025 | Reading Time: 4 min read

Table of Contents

Introduction

clrscr in C stands for Clearing the console screen in C programming. It is a fundamental function utilised by developers to maintain a clean interface.

In console applications, the ability to clear the screen is crucial for maintaining a clean and user-friendly interface. As programs output dynamic content, a cluttered screen can confuse users, making it difficult to focus on the latest information. By clearing the screen, the interface with the new screen, helps users interact with the program more efficiently and comfortably.

This function erases all previous outputs, allowing developers to display new information without distractions.

What is clrscr in C?

The clrscr() is a predefined function in C used to clear the console screen. It removes all previous text, giving you a clean screen for new outputs. It is defined in the conio.h library, and primarily used in older compilers like Turbo C.

Syntax

void clrscr(void);

Example

#include <stdio.h>
#include <conio.h>

int main() {
    clrscr(); // Clear the screen
    printf("Screen cleared!\n");
    return 0;
}

Output

Screen cleared!

Explanation

Function Return Type and Parameters

clrscr() does not take any parameters and does not return a value; it simply performs its operation when called. The clrscr() function is defined in the conio.h header file is a simple method for clearing the console screen in C programming.

What is conio.h?

The conio.h library originated in the early days of DOS-based programming. It was designed to facilitate console input and output operations, providing functions like getch() and clrscr() for better user interaction.

To use clrscr(), you must include the conio.h header file at the beginning of your C program:

#include <conio.h>

Code Example Without clrscr in C

#include <stdio.h>

int main() {
    // Print the message with a line break between the parts
    printf("Hello Students,\n");
    printf("welcome to Nxtwave learning!\n");

    return 0;
}

Output

Hello Students,
welcome to Nxtwave learning!

Explanation

The above program prints Hello Students and the next message starts from a new line and prints the Welcome to Nxtwave learning.

Code Example With clrscr in C

#include <stdio.h>
#include <conio.h> // Include conio.h for clrscr()

int main() {
    // Print the first part of the message
    printf("Hello Students,\n");

    // Clear the screen after the first message
    clrscr();

    // Print the second part of the message
    printf("welcome to Nxtwave learning!\n");

    return 0;
}

Output

Hello Students,
welcome to Nxtwave learning!

Explanation

The program begins by printing the message "Hello Students,". After this, it clears the screen using clrscr(), so that any content already displayed is removed. Once the screen is cleared, the program prints the second part of the message, "welcome to Nxtwave learning!", on a clean screen.

Examples of clrscr() in C Programming

Here are a few examples codes using clrscr in C:

Example 1: Simple Use of clrscr()

#include <conio.h>  // Include the conio.h header
#include <stdio.h>

int main() {
    clrscr();  // Clears the console screen
    printf("The screen has been cleared!");
    return 0;
}

Output

//The screen will be cleared before the message is displayed, and the output will appear mentioned below
The screen has been cleared!

Explanation

Example 2: Using clrscr() with Input and Output

#include <conio.h>
#include <stdio.h>

int main() {
    int num1, num2, sum;

    clrscr();  // Clears the screen

    // Get user input
    printf("Enter two numbers: ");
    scanf("%d%d", &num1, &num2);

    sum = num1 + num2;  // Calculate the sum

    // Display the result
    printf("Sum of the numbers is: %d\n", sum);

    return 0;
}

Output

// screen will be cleared
Enter two numbers: 5, 10
Some of the numbers is: 15

Explanation

Example 3: Using clrscr() in a Loop

#include <conio.h>  // Include the conio.h header
#include <stdio.h>
#include <unistd.h> // For sleep function

int main() {
    for (int i = 0; i < 5; i++) {
        clrscr();  // Clear the screen
        printf("Clearing the screen... %d\n", i + 1);
        sleep(1);  // Wait for 1 second
    }
    return 0;
}

Output

Clearing the screen... 1
(After 1 second, screen clears)
Clearing the screen... 2
(After 1 second, screen clears)
...
Clearing the screen... 5

Explanation

Example 4: Multiple clrscr() Calls in a Loop

#include <conio.h>  // For clrscr()
#include <stdio.h>
#include <unistd.h>  // For sleep()

int main() {
    for (int i = 0; i < 5; i++) {
        clrscr();  // Clear screen
        printf("Iteration %d\n", i + 1);
        sleep(1);  // Wait for 1 second
    }
    return 0;
}

Output

Iteration 1
(After 1 second, screen clears)
Iteration 2
(After 1 second, screen clears)
...
Iteration 5

Explanation

This program clears the screen 5 times within a loop, prints the iteration number, and then pauses for 1 second before removing the screen again.

Example 5: Clearing Screen Before Displaying a Menu

#include <conio.h>
#include <stdio.h>

int main() {
    int choice;

    clrscr();  // Clears the screen

    // Display the menu
    printf("Menu:\n");
    printf("1. Option 1\n");
    printf("2. Option 2\n");
    printf("3. Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    return 0;
}

Output

Menu:
1. Option 1
2. Option 2
3. Exit
Enter your choice: 2

Explanation

How Many Ways to Clear Your Screen?

There are three ways to clear your screen in C programming:

Method 1: Using clrscr()

#include <conio.h>
#include <stdio.h>

int main() {
    clrscr();  // Clears the screen
    printf("It's clean screen!");
    return 0;
}

Output

//It will clear the screen first, then display the message below.
It's clean screen!

Explanation

Method 2: Using system("clear") or system("cls")

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

int main() {
    system("cls");  // Windows
    // OR
    system("clear");  // Linux/Unix
    printf("Nxtwave!");
    return 0;
}

Output

// It will clear the screen first and then display the message below.

Nxtwave!

Explanation

The system("cls") clears the screen on Windows, while system("clear") does so on Linux/Unix. The system() function calls the OS shell to execute the command. After clearing, it displays the Nxtwave on the screen.

Method 3: Using ANSI Escape Sequences (\e[1;1H\e[2J)

printf("\e[1;1H\e[2J");

printf("Nxtwave!");

Output

//It will clear the screen first, and then display the message below.
Nxtwave!

Explanation

Tips for Developers While Using clrscr() Function

The following are some tips developers need to ensure when using the clrscr() function in the C language:

Advantages and Disadvantages of Clrscr() function in C

Here are the advantages and disadvantages of clrscr() in C:

Advantages

Disadvantages

Conclusion

In conclusion, the clrscr() function in C programming is a way to clear the console screen. It improves user experience in interactive applications, such as menus and games, by providing a clean display of new information. However, clrscr() is not part of the C standard library and may not be supported across all platforms; developers should consider modern alternatives for cross-platform compatibility.

Frequently Asked Questions

What is clrscr() used for in C?

clrscr() is used to clear the console screen. It helps to remove any previous text on the screen, making it easier to display fresh output. It's beneficial when working with old compilers like Turbo C.

What are some common errors with clrscr()?

Missing header files or misusing it within program logic can lead to errors.

What if my compiler does not support clrscr()?

If your compiler does not support clrscr(), you can use other methods to clear the screen, like system commands. For example, on Windows, you can use system("cls"), and on Unix/Linux, you can use system("clear").

What are the system requirements for clrscr()?

clrscr() is primarily supported on DOS and Windows systems. Its functionality may not be available or behave differently on Unix/Linux platforms.

What is the difference between getch() and clrscr()?

Which library is clrscr() in?

clrscr() is part of the conio.h library is a non-standard function, mainly used in older compilers like Turbo C.

Related Articles


About NxtWave

NxtWave is a learning platform offering industry-recognized certifications and career development programs for students and professionals in technology fields.

Contact Information: