Wednesday, 16 November 2011

How do I position the cursor on the screen?in C programming

How do I position the cursor on the screen?

There is no method in the C standard for positioning the cursor on the screen. There are many reasons for this omission. C is designed to work across a broad range of computers, many of which have different screen types. On a line terminal, for example, it is impossible to move the cursor up. An embedded system might even be written in C, in which case there might not be a screen at all!

That being said, there is still a use for positioning the cursor on the screen in your own programs. You might want to give the user an attractive visual that is possible to display only by moving the cursor around. Or you might even want to attempt a little animation using the print commands. Despite the lack of standards for this action, there are several ways the problem can be addressed.

First, the writer of the compiler can supply a library of routines that handle screen output specific to that compiler. One of these routines will certainly be the positioning of the cursor. This is arguably the worst solution, because every manufacturer is free to make his own implementation. Therefore, a program written with one compiler will almost certainly need to be rewritten if it moves to another compiler, much less another machine.

Second, a standard set of library functions can be defined that the compiler writer can implement for his
compiler. This is the root of the popular curses package. Curses is available for most machines and compilers.
Therefore, a program written to use curses for screen output works on most other computers and compilers. Third, you can use the fact that the device to which you are printing can interpret the characters you are sending in a particular way. There is a standard way in which terminals (or screens) should be made to interpret characters sent to them, the ANSI standard. If you assume that your computer is ANSI compliant,

you can print the right characters to manipulate your screen into positioning the cursor in the places you want, among other actions.

Cross Reference:

None.

No comments:

Post a Comment