The clrscr() function was used to clear the MS-DOS console screen in older C compilers like Turbo C and Turbo C++. clrscr() is not a standard C function—if you try to compile a program that includes clrscr() in a modern compiler like GCC or Clang, you’ll get an error that says “function not declared” or “not declared in this scope." So what if you need to clear the console in your program? This minHour article will teach you how to replace clrscr() with the system() function to clear the screen in C.
Add the stdlib.h header file to your code.
The system() function is used to pass commands to the terminal or console, and it’s declared in the stdlib.h header file.
- clrscr() is defined in the conio.h header file. Since we’ll be removing clrscr() and replacing it with system(), you can remove the conio.h header file.
Replace clrscr() with system(“cls”) on Windows.
The cls command, when run at the Windows command prompt, clears the console screen. Passing cls through the system() function effectively clears the screen.
Replace clrscr() with system(“clear”) on Linux or macOS.
The system() function will pass the clear command to the console. The Linux command (and thus the macOS command) to clear the console is clear, so system(“clear”) will clear the console window.