The function `clrscr()` is used to clear the console screen...
The function clrscr()
is used to clear the console screen in C and C++ programs. It is typically available in the conio.h
header file, which is mainly used in DOS-based systems or systems supporting Turbo C/C++.
When clrscr()
is called, it clears the text displayed in the terminal or console window, providing a blank screen to display further program output.
Important Notes:
-
Header File:
clrscr()
requires the inclusion of theconio.h
header file.#include <conio.h>
-
Platform-Specific:
clrscr()
is not standard in modern compilers and environments, asconio.h
is not supported in many contemporary systems like GCC, Clang, or Visual Studio. -
Alternatives: For non-DOS systems, clearing the screen is often done using platform-specific commands, such as:
- Windows: Calling
system("cls");
- Linux/macOS: Calling
system("clear");
- Windows: Calling
Example Usage:
#include <conio.h>
int main() {
printf("Hello, World!");
getch(); // Wait for a key press
clrscr(); // Clears the screen
printf("The screen was cleared!");
return 0;
}
In the above code:
- The console first displays "Hello, World!".
- After a key is pressed, the screen is cleared using
clrscr()
, and "The screen was cleared!" is displayed on a clean screen.
This function is mainly of historical interest, given its limited support in modern programming environments.