ANSI escape codes let you control the terminal from a standard C program — hide or show the cursor, move it to any position, and print colored or styled text. Unlike the old DOS graphics.h approach that only worked in Turbo C, ANSI escape codes work on any POSIX terminal: Linux, macOS, and modern Windows terminals. No extra library needed — just stdio.h and printf.
What Are ANSI Escape Codes?
An ANSI escape sequence is a string starting with ESC (ASCII 27, written as \033 in C) followed by [ and a command. The terminal intercepts these sequences and acts on them rather than printing them as text.
ESC [ ?25l │ │ └─ command: hide cursor │ └─ CSI (Control Sequence Introducer) └─ ESC character (\033)
In C source code, ESC is '\033' (octal) or '\x1b' (hex) — both are identical.
Program 1 – Hide and Show the Cursor
/* Hide and restore the terminal cursor using ANSI escape codes */
#include <stdio.h>
#define HIDE_CURSOR "\033[?25l"
#define SHOW_CURSOR "\033[?25h"
int main(void)
{
printf("Step 1: Cursor is visible.\n");
printf(HIDE_CURSOR);
printf("Step 2: Cursor is now hidden.\n");
printf("Step 3: Still printing without a visible cursor.\n");
printf(SHOW_CURSOR);
printf("Step 4: Cursor restored.\n");
return 0;
}
What You See in the Terminal
Step 1: Cursor is visible. Step 2: Cursor is now hidden. Step 3: Still printing without a visible cursor. Step 4: Cursor restored.
Between steps 1 and 4, the blinking cursor in your terminal disappears. Always restore it before the program exits — if you don’t and the program crashes, the cursor stays hidden until you run reset.
Program 2 – Move the Cursor
Cursor movement lets you update specific cells on screen without clearing everything — useful for status bars, progress indicators, and simple text UIs.
/* Move cursor to absolute and relative positions */
#include <stdio.h>
int main(void)
{
/* Print a 5x5 grid of dots */
int r, c;
for (r = 0; r < 5; r++) {
for (c = 0; c < 5; c++)
printf(". ");
printf("\n");
}
/* Move cursor up 3 rows and overwrite the middle cell */
printf("\033[3A"); /* 3 rows up */
printf("\033[4C"); /* 4 cols right */
printf("X"); /* overwrite one dot */
/* Return to bottom so shell prompt appears correctly */
printf("\033[3B"); /* 3 rows down */
printf("\033[0G"); /* column 1 */
printf("\n");
return 0;
}
What You See in the Terminal
. . . . . . . . . . . . X . . . . . . . . . . . .
The grid is printed top-to-bottom, then the cursor jumps back up 3 rows and right 4 columns to overwrite the dot at row 3, column 3 with X. The grid itself is never reprinted.
Program 3 – Text Colors and Styles
/* ANSI text colors and styles */
#include <stdio.h>
int main(void)
{
/* Text styles */
printf("\033[1mBold\033[0m ");
printf("\033[4mUnderline\033[0m ");
printf("\033[7mReverse\033[0m\n");
/* Foreground colors */
printf("\033[30mBlack\033[0m ");
printf("\033[31mRed\033[0m ");
printf("\033[32mGreen\033[0m ");
printf("\033[33mYellow\033[0m ");
printf("\033[34mBlue\033[0m ");
printf("\033[35mMagenta\033[0m ");
printf("\033[36mCyan\033[0m ");
printf("\033[37mWhite\033[0m\n");
/* Background colors */
printf("\033[41m Red BG \033[0m ");
printf("\033[42m Green BG \033[0m ");
printf("\033[43m Yellow BG \033[0m ");
printf("\033[44m Blue BG \033[0m\n");
return 0;
}
What You See in the Terminal
Each word or phrase appears in the indicated style or color. \033[0m resets all attributes back to the terminal default after each word — without the reset, colors carry forward and affect all subsequent output.
How to Compile and Run
gcc -ansi -Wall -Wextra cursor_hide.c -o cursor_hide
./cursor_hide
These programs require a POSIX terminal that supports ANSI escape codes — any Linux terminal, macOS Terminal/iTerm2, or Windows Terminal on Windows 10+. They will not work in the raw Windows cmd.exe console without enabling VT100 mode first.
ANSI Escape Code Quick Reference
| Code | Effect |
|---|---|
\033[?25l |
Hide cursor |
\033[?25h |
Show cursor |
\033[nA |
Move cursor up n rows |
\033[nB |
Move cursor down n rows |
\033[nC |
Move cursor right n columns |
\033[nD |
Move cursor left n columns |
\033[row;colH |
Move to absolute position (1-based) |
\033[2J |
Clear entire screen |
\033[2K |
Clear current line |
\033[H |
Move cursor to top-left (home) |
\033[0m |
Reset all attributes |
\033[1m |
Bold |
\033[4m |
Underline |
\033[7m |
Reverse video |
\033[30m–\033[37m |
Foreground: black, red, green, yellow, blue, magenta, cyan, white |
\033[40m–\033[47m |
Background: same color order |
\033[90m–\033[97m |
Bright foreground colors (most terminals) |
Related Programs
- Recursion in C – Complete Guide
- Pointers in C – Complete Guide
- Binary Search in C
- BFS Algorithm in C
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
Practice C with the C Programming Quiz App — 500+ MCQs covering arrays, pointers, terminal I/O and more.
Download on Google Play →
3 comments on “ANSI Escape Codes in C – Control Terminal Cursor, Colors, and Position”
The post is absolutely fantastic! Lots of great information and inspiration both of which we all need! Also like to admire the time and effort you put into your blog and detailed information you offer! I will bookmark your blog!
oklahoma city roofing
Thanks for your reply. Please avoid posting back links to unrelated websites.
Excellent read I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!
http://www.fadeplex.com