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." [1] X Research source So what if you need to clear the console in your program? This wikiHow article will teach you how to replace clrscr() with the system() function to clear the screen in C.
Steps
-
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. [2] X Research source
- 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.
-
2Replace clrscr() with system("cls") on Windows. The cls command, when run at the Windows command prompt, clears the console screen. [3] X Research source
Passing cls through the system() function effectively clears the screen. [4] X Research source
-
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. [5] X Research source
Expert Q&A
Tips
References
About This Article
clrscr() is not a standard C function. It was only used to clear MS DOS consoles in Borland compilers like Turbo C. If you want to clear the console screen in C, you'll want to use system("clear") for Linux and macOS, or system("cls") for Windows.