How do I clear the screen in C?
Solution 1
The best way to clear the screen is to call the shell via system(const char *command)
in stdlib.h:
system("clear"); //*nix
or
system("cls"); //windows
Then again, it's always a good idea to minimize your reliance on functions that call the system/environment, as they can cause all kinds of undefined behavior.
Solution 2
You need to check out curses.h. It is a terminal (cursor) handling library, which makes all supported text screens behave in a similar manner.
There are three released versions, the third (ncurses) is the one you want, as it is the newest, and is ported to the most platforms. The official website is here, and there are a few good tutorials.
#include <curses.h>
int main(void)
{
initscr();
clear();
refresh();
endwin();
}
Solution 3
Windows:
system("cls"); // missing 's' has been replaced
Unix:
system("clear");
You can wrap this in a single, more portable piece of code like so:
void clearscr(void)
{
#ifdef _WIN32
system("cls");
#elif defined(unix) || defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
system("clear");
//add some other OSes here if needed
#else
#error "OS not supported."
//you can also throw an exception indicating the function can't be used
#endif
}
Note the check for unix is pretty expansive. This should also detect OS X, which is what you're using.
Solution 4
The availability of this function or similar ones like clrscn() are very system dependent and not portable.
You could keep it really simple and roll you own:
#include <stdio.h>
void clearscr ( void )
{
for ( int i = 0; i < 50; i++ ) // 50 is arbitrary
printf("\n");
}
user2651382
Updated on October 15, 2020Comments
-
user2651382 over 3 years
I want to clear all the text that is on the screen. I have tried using:
#include <stdlib.h> sys(clr);
Thanks in advance! I'm using OS X 10.6.8. Sorry for the confusion!
-
JSQuareD almost 11 yearsWhat OS are you using?
-
Tarik almost 11 yearsDuplicate question. Please check.
-
Paul Renton almost 11 yearsI believe this question's answer will depend on the OS you are using. You need to edit the question and tell us which OS you are using
-
Mike D almost 11 yearsRelated: stackoverflow.com/q/228617/620197
-
Crowman almost 11 yearsPlease consider the wisdom of doing this, when you find an answer. Many users, me included, don't appreciate programs randomly clearing their screens, or going off and doing something when I've pressed a key but before I've been given the opportunity to confirm it's the input I wanted to give by pressing Enter. Unless you're using an actual TUI, this kind of thing is never necessary, and usually just annoying.
-
Sid almost 11 yearsSo this function
clrscr();
of the conio.h library wont work in C? I used it so many times. -
Commander Worf almost 11 years@Sid:
clrscr()
isn't in stdio.h, it's in conio.h, which isn't POSIX compliant or available on most (edit) compilers. -
Crowman almost 11 years@Sid:
clrscr()
may be in yourstdio.h
header (stdio.h
is not a "library"), but it's not in C's. -
Sid almost 11 yearsThanks for clarification!
-
-
tallen almost 11 yearsWhy would you down vote this, curses is a way to do this
-
brianmearns almost 11 yearsThat's not C, it's C++. And that doesn't clear the screen, just fills it with blank lines. Two key differences being that the cursor ends up at the bottom of 50 empty lines (instead of at (0,0)) and that the scrollable buffer is not erased as it would be if you cleared the screen.
-
brianmearns almost 11 yearsNot sure who downvoted it, but
curses
is a way to do it, not the way. -
Sam I am says Reinstate Monica almost 11 yearsThis might qualify as a comment, but it most definitely is not an answer, and is unlikely to help the OP. Maybe if you wrote what
curses.h
was and/or why he would check outcurses.h
it would be better -
David Elliman almost 11 yearsIt compiles as C for me. It works. Most command line programs do not use cursor addressing.
-
tallen almost 11 yearsas such it is an answer
-
Sam I am says Reinstate Monica almost 11 years@tallen as is is as not an answer. It may be a hint, but the button doesn't say "post your hint" it says "post your answer"
-
JSQuareD almost 11 yearsI downvoted because: 1) You didn't explain what curses.h is and didn't even give a link. 2) you didn't give a code example. 3) curses.h is not a standard library, and I think it's even a Unix-specific example, making this solution even less portable than the standard solution. 4) including an entire (non-standard) library just for clearing the screen might be a bit of an overkill.
-
Jiminion almost 11 yearsMore information added.
-
tallen almost 11 yearsthis is the way you did it before the VT terminal!
-
Edwin Buck almost 11 yearsWhat a waste, you don't fork a full process for do this, and why introduce the race condition?
-
Jiminion almost 11 years+1 to Edwin for bringing up the process issue.
-
wildplasser almost 11 yearsThis is in fact the only truly portable solution. (but it could cost a lot of paper on your TTY ;-)
-
Commander Worf almost 11 yearsHow would you do it? Including the ncurses library is a bit overkill IMO, and one 'clear' process will hardly slow down most programs.
-
Jiminion almost 11 yearsUses system call => extra process. Hmm.....
-
Edwin Buck almost 11 yearsIf you think adding a library is overkill, try running a program. It adds all the libraries used to develop the program. Also, since, that program is a shell it then interprets the arguments, does a look-up for the program on the path, and then launches another program. All of that for a screen clear? ncurses is a far better choice, and if someone slips in a rogue copy of "clear" or "cls", or has the ability to reorder their path, you didn't just introduce a security hole.
-
gravitas almost 11 yearsCan someone explain to me the race-condition this introduces and how forking would prevent it?
-
Edwin Buck almost 11 yearsYou avoid it by not running two programs to do one job. If you decided to go with this solution, then you avoid it by polling the operating system to ensure that the clear program has exited cleanly before writing any further text to the screen (which would require a write-lock in your program). This "simple" solution is very problematic, to the point it's not simple to fix. Use the solution (documented by Jim) that costs you an extra five lines of code, but isn't subject to this race condition, because all of your screen logic is in one process.
-
JSQuareD almost 11 years@Jim Good point, but it is the most no-nonsense way to do this. And in a simple console-application making a system-call every now and then will hardly be an issue for performance.
-
Jiminion almost 11 yearsYes. I can see how both solutions have their place.
-
Edwin Buck almost 11 yearsI added a bit more info, for those who can't be bothered to google.
-
Jiminion almost 11 years@EdwinBuck Thank you. I guess ncurses is the best choice at this point.
-
JSQuareD almost 11 years@edwinbuck exactly what makes this unportable? Also, whatever OS you're using, stdlib.h is the only header that needs to be included, so no preprocessor logic is required there.
-
Edwin Buck almost 11 years@JSQuareD You are right about the header includes. My apologies. The part that I meant to express in my complaints is that it assumes a very particular environment configuration, which while stock, is easily modified way past deployment. In that sense, it isn't functionally assured unless you can control the execution environment, over the entire life of the program. Not worth the hassle in my opinion, but you are right, the source code is portable.
-
JSQuareD almost 11 years@edwinbuck hmm.. I guess. But I find it hard to believe that people would undefine these macros or modify the meaning of cls or clear. And if they did, it's hardly the source code that's to blame. On any rate, I'm not saying curses isn't a more portable/elegant solution, I just think it's a bit of an overkill if you just want to clear the screen. Plus, it might be a fair bit of work to implement into an existing console application.
-
brianmearns almost 11 years@DavidElliman: It compiles as C because you changed it =J.
cout <<
like you originally had is not C as you cannot do operator overloading in C. My point about it not actually clearing the screen was that your cursor is left at the bottom of the console instead of the top. It's not just a matter of cursor addressing, it's a very definite cosmetic difference. When you clear the screen and start typing, you expect it to appear on the first line, not the last. It may be functionally the same in most cases, but it looks and feels very different to the user. -
Admin almost 5 years
system("cls || clear");