user466534
user466534

Reputation:

Function clrscr in C and C++

Does today's C or C++ compilers use the clrscr system function?

Upvotes: 9

Views: 17707

Answers (5)

Janus Troelsen
Janus Troelsen

Reputation: 21298

On Unix-like systems you can use VT100 escape codes.

std::cout << "\033[2J" << std::flush;

See http://www.termsys.demon.co.uk/vtansi.htm

Upvotes: -1

pmg
pmg

Reputation: 108988

DeathStation 9000 and its ZOG C compiler still use clrscr().

quote from http://dialspace.dial.pipex.com/town/green/gfd34/art/

It would be unfortunate if any more lives were lost simply because some programmers feel a deep spiritual need to obliterate the display device, and much much more, using ZOG C's Commence Launch (Remote Systems Console Request) function, clrscr().

Upvotes: 0

alecov
alecov

Reputation: 5171

Also, as an alternative to conio.h, you can try using ncurses, which provides terminal handling, cursor management, colors and a lot of other functionalities. In particular, it provides the clear() function with a similar functionality to the clrscr() function you mentioned. For Windows (which must be your case), there is PDCurses that employs the same API. In particular, ncurses complies with the XSI Curses base specification, and it is widely adopted; you should stick to it if any degree of portability matters.

Upvotes: 0

Prasoon Saurav
Prasoon Saurav

Reputation: 92884

clrscr() is a nonstandard function (neither mentioned in ISO C99 nor in ISO C++-98) defined in <conio.h> (which is not standard compliant itself). However some compilers (like Turbo C/C++) support it as an extension.

Upvotes: 13

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215507

Like all of the stuff in conio.h. clrscr() has nothing to do with standard C. conio is a common API of ancient DOS-based C implementations for lower-level console io - things like clearing the screen, moving the cursor, reading individual keystrokes, etc. I don't know the history but presumably it dates back to before DOS had ANSI.SYS to support standard terminal-escape codes for cursor positioning, clearing the screen, changing colors, ...

If you're just playing around learning C, there's no harm in using the conio functions, but you should avoid making a habit of #include <conio.h>. In most of the questions I've seen on SO where conio.h was included, it wasn't even being used... This kind of bad habit leads to senselessly nonportable code.

Upvotes: 8

Related Questions