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
- The function clrscr() from the conio.h library is called to clear the console screen
- After clearing the screen, the program prints "Screen cleared!".
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 (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 (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,
- The screen is cleared immediately after this first message is printed, so you won't see it anymore.
- Then, after the screen is cleared, you will see:
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
- The clrscr() function clears the console screen.
- After clearing, the message "The screen has been cleared!" is displayed.
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:
- The program clears the screen using clrscr().
- It then prompts the user to enter two numbers.
- After the numbers are entered, the sum is calculated and displayed.
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
- The program runs a loop 5 times, clearing the screen in each iteration using clrscr().
- After clearing, it prints the message "Clearing the screen..." followed by the current loop iteration number.
- The sleep(1) function pauses the program for 1 second before the next iteration.
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
- The screen is cleared using clrscr().
- A menu with three options is displayed to the user, and the program waits for input.
How Many Ways to Clear Your Screen?
There are three ways to clear your screen in C programming such as:
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
- clrscr() clears the screen when it is called. It removes any previous text from the console.
- After clearing the screen, printf("It’s clean screen!") displays the message "It’s clean screen!"
2. Using system("clear") or system("cls")
- For Linux/Unix: system("clear")
- For Windows: 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
3. Using Regex (\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
- \e: Escape character that begins the sequence.
- [1;1H: Moves the cursor to the top-left corner (row 1, column 1).
- [2J: Clears the entire screen.
- This platform-independent method works on Linux, macOS, and Windows (with ANSI escape support). After clearing, it displays the message "Nxtwave!
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:
- Use it when resetting the console display before showing new information or results.
- Avoid using it excessively as it can disrupt user flow and may not be necessary for every output.
- Keep calls to clrscr() organised within your code structure to maintain readability.
- Implement error handling around user inputs or critical operations to ensure smooth execution even when errors occur.
- Frequent use of clrscr() may introduce performance overhead; consider alternatives if performance becomes an issue.
- Refer to documentation or community forums for specific issues related to your compiler or environment settings.
Advantages and Disadvantages of Clrscr() function in C
Here are the advantages and disadvantages of clrscr() in c:
Advantages
- It’s easy and quick way to clear the screen without much complexity.
- It helps provide a clean and organised user interface, especially when the output changes frequently.
Disadvantages
- Errors often arise from missing header files or incorrect usage of functions.
- Not part of standard C libraries; limits portability.
- Frequent calls can slow down applications if used excessively.
- It is iplatform-dependent and may not work consistently across all systems.
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.
Learn Industry-Relevant Skills in College for a Strong Tech Career Start!
Explore ProgramFrequently Asked Questions
1. 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.
2. What are some common errors with clrscr()?
Missing header files or misusing it within program logic can lead to errors.
3. 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").
4. 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.
5. What is the difference between getch() and clrscr()?
- getch() waits for a user to press a key. It often pauses the program, so it doesn't close immediately.
- clrscr() clears the screen, so any text displayed earlier is erased, leaving the console empty for new output.
6. 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.