Cursor and color setting in C

0 votes
asked Feb 20 by Alberto

Hello,

I teach both Pascal and C at school, and I need some cursor control in order to make my examples more interesting.

With Pascal I simply add the well-know old Borland library "USES CRT;"  and I get:

clrscr; to clear the screen

gotoxy(X,Y); to set the cursor position on screen

textcolor(n); textbackground(n); to set the color for pen and background

Now I'm not able to find a way to set the cursor with C language. In theory, #include <conio.h> will include the same Borland library. The strange fact is the GDB ONLINE accept it like a valid library. But then, some commands will be accepted as valid but with no effects (for example "clrscr" is accepted but will not clear the screen) Some other commands (for example "gotoxy") will be refused as "undefinited reference". Very strange...

Anyway, is there a solution to make it works? Or is there another library to get some cursor and text color control?

1 Answer

0 votes
answered Mar 5 by Matthias from Erlangen
edited Mar 5
Google: ANSI Escape Sequences
http://ascii-table.com/ansi-escape-sequences.php

colors:

printf("\e[31mHallo \e[32mwie \e[34mgehts?\n");

clearscreen and move to the left upper corner:

printf("\e[2J\e[H");

move to coordinate line 3 - simicolon - column 40

printf(\e[3;40H");

The Escape-Sequence is
'\e' (char) = 0x1B (hex) = 27 (dez)
 '[' (char)  = 0x5B (hex) = 88 (dez)
and works only for ANSI compatible terminal-emulations

with that, you can create your own clrscr(), textcolor(c) or gotoxy(x,y) functions

good luck ;)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...