Interview Questions

How can I clear the screen? How can I print text in color? How can I move the cursor to a specific x, y position?

C Interview Questions and Answers


(Continued from previous question...)

How can I clear the screen? How can I print text in color? How can I move the cursor to a specific x, y position?

Such things depend on the terminal type (or display) you're using. You will have to use a library such as termcap, terminfo, or curses, or some system-specific routines, to perform these operations.
Functions in the curses library to look for are clear, move, standout/standend, and attron/attroff/attrset; the last three work with attribute codes such as A_REVERSE. In MS-DOS libraries, there are typically functions named gotoxy and clrscr or _clearscreen; you can also use the ANSI.SYS driver or low-level interrupts. Under termcap or terminfo, use tgetstr to retrieve strings like cl, so/se, and cm for clear screen, standout mode, and cursor motion respectively, then output the strings; using cm additionally requires calling tgoto. Some baroque terminals require attention to other ``capabilities'' as well; study the documentation carefully. Be aware that some older terminals may not support the desired capabilities at all.
Most modern terminal emulation schemes support the ANSI escape sequences for cursor motion and visual attributes, so if you're willing to sacrifice portability, you can print those sequences directly. Here is a tiny example to whet your appetite:
printf("\033[2J"); /* clear screen */
printf("\033[%d;%dH", 10, 20); /* move cursor (row 10, col 20) */
printf("Hello, ");
printf("\033[7mworld\033[0m!"); /* inverse video */

some more explanation, and brief lists of codes.
``ANSI escape sequences'' are special sequences of characters which do not print but which rather affect a display in some way. The basic syntax of an ANSI escape sequence is
ESC [ parameter(s) code
where ESC is the ASCII ``Escape'' character ('\033' or '\x1B'), [ is a single open square bracket character, parameter(s) is a numeric parameter or semicolon-separated list of numeric parameters, and code is a single character indicating the action to be performed. A (brief and incomplete) list of codes is:
H move cursor; the two parameters are the row and column to move to
J clear screen; the parameter 2 means clear the whole screen
m set graphic rendition (see parameter list below)

The graphic rendition codes include:
0 return to normal (no attributes)
1 bold
4 underline
7 inverse video
31 red
32 green
33 yellow
34 blue
35 magenta
36 cyan

There are also codes in the 40's for setting the background color, e.g. 41 sets the background to red.

The portable way of emitting these sequences (if you're not going whole-hog and using curses) is to use termcap or terminfo; here is an example.
The old termcap library routines, and their later ``terminfo'' replacements, involve a database of device-dependent strings to be ``printed'' to achieve various results. A program first loads the description of the terminal currently in use (typically named by the environment variable TERM), then fetches the various control strings and other parameters it will need, and finally prints those strings as needed. When moving the cursor, the tgoto function takes care of encoding the desired row and column number using whatever device-dependent encoding scheme is described in the device-dependent cm (``cursor motion'') string.
Here is a minimal example, which omits error checking and other nuances, but should get you started:
#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>

int main()
{
char buf[1024];
char buf2[30];
char *ap = buf2;
char *clearstr, *gotostr, *standstr, *stendstr;

tgetent(buf, getenv("TERM"));

clearstr = tgetstr("cl", &ap);
gotostr = tgetstr("cm", &ap);
standstr = tgetstr("so", &ap);
stendstr = tgetstr("se", &ap);

fputs(clearstr, stdout);
fputs(tgoto(gotostr, 20, 10), stdout);
printf("Hello, ");
fputs(standstr, stdout);
printf("world");
fputs(stendstr, stdout);
putchar('!');
}
The portable way of emitting these sequences (if you're not going whole-hog and using curses) is to use termcap or terminfo; here is an example.
For clearing the screen, a halfway portable solution is to print a form-feed character ('\f'), which will cause some displays to clear. Even more portable (albeit even more gunky) might be to print enough newlines to scroll everything away (although of course this leaves the cursor at the bottom of the screen, not the top). As a last resort, you could use system to invoke an operating system clear-screen command.

(Continued on next question...)

Other Interview Questions