简体   繁体   中英

Function clrscr in C and C++

今天的C或C ++编译器是否使用clrscr系统函数?

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.

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.

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.

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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM