clrscr in C
Function «clrscr» (works in Turbo C++ compiler only) clears the screen and moves the cursor to the upper left-hand corner of the screen. If you are using the GCC compiler, use system function to execute the clear/cls command.
C programming code for clrscr
int main ( )
<
printf ( «Press any key to clear the screen. \n » ) ;
printf ( «This appears after clearing the screen. \n » ) ;
printf ( «Press any key to exit. \n » ) ;
getch ( ) ;
return 0 ;
>
In the program, we display the message (Press any key to clear the screen) using printf and ask the user to press a key. When the user presses a key screen will be cleared and another message will be printed. Function clrscr doesn’t work in Dev C++ compiler. Use the cleardevice function instead of clrscr in graphics mode.
Clear screen in C without using clrscr
int main ( )
<
printf ( «Press any key to clear the screen \n » ) ;
getchar ( ) ;
system ( «clear» ) ; // For Windows use system(«cls»);
return 0 ;
>
clrscr Function in c Programming
clrscr() function clears the screen and moves the cursor to the upper-left-hand corner of the screen. It is defined in conio.h header file. This function is optional. If the function is to be used, then place it after variable and function declaration only.
This function is only available in windows systems. You wont find it in c compilers of UNIX based and Mac systems.
Prototype
Return Type
This function returns nothing.
Example
Below is the program explains how to use clrscr in c?
Output
Error clrscr Should have a Prototype. How to solve it?
- First check that you have included conio.h header file in your program.
- If your operating system is other than windows this header file is not supported.
- Check for the compiler documentation regarding the presence of conio.h header file.
People Also Ask
What is the use of clrscr()?
clrscr() clears the output of previously executed program from the console.
How to use clrscr in c
Include conio.h header file. Then call clsrcr() in main function or in any other user defined function.
How do you clear the console screen in C?
Is there a «proper» way to clear the console window in C, besides using system(«cls») ?
![]()
14 Answers 14
This function will work on ANSI terminals, demands POSIX. I assume there is a version that might also work on window’s console, since it also supports ANSI escape sequences.
There are some other alternatives, some of which don’t move the cursor to <1,1>.
![]()
Well, C doesn’t understand the concept of screen. So any code would fail to be portable. Maybe take a look at conio.h or curses, according to your needs?
Portability is an issue, no matter what library is used.
![]()
For portability, try this:
Then simply call clrscr() . On Windows, it will use conio.h ‘s clrscr() , and on Linux, it will use ANSI escape codes.
If you really want to do it «properly», you can eliminate the middlemen ( conio , printf , etc.) and do it with just the low-level system tools (prepare for a massive code-dump):
![]()
A workaround tested on Windows(cmd.exe), Linux(Bash and zsh) and OS X(zsh):
Using macros you can check if you’re on Windows, Linux, Mac or Unix, and call the respective function depending on the current platform. Something as follows:
Since you mention cls , it sounds like you are referring to windows. If so, then this KB item has the code that will do it. I just tried it, and it worked when I called it with the following code:
There is no C portable way to do this. Although various cursor manipulation libraries like curses are relatively portable. conio.h is portable between OS/2 DOS and Windows, but not to *nix variants.
The entire notion of a "console" is a concept outside of the scope of standard C.
If you are looking for a pure Win32 API solution, There is no single call in the Windows console API to do this. One way is to FillConsoleOutputCharacter of a sufficiently large number of characters. Or WriteConsoleOutput You can use GetConsoleScreenBufferInfo to find out how many characters will be enough.
You can also create an entirely new Console Screen Buffer and make the current one.