Print colored message with different fonts and sizes in C

In C programming, the graphics.h library provides functions to customize text output with different colors, fonts, and sizes. This library is primarily used in graphics programming to create visually appealing text displays on the screen.

Note: The graphics.h library requires Turbo C/C++ or compatible compilers. For modern compilers, you may need to install graphics libraries like WinBGIm or use alternative approaches.

Key Graphics Functions for Text Formatting

1. setcolor() Function

The setcolor() function changes the color of the output text −

Syntax

void setcolor(int color);

Example

#include <stdio.h>
#include <graphics.h>
#include <conio.h>

int main() {
    int gdriver = DETECT, gmode;
    initgraph(&gdriver, &gmode, "C:\TurboC3\BGI");
    
    setcolor(RED);
    outtextxy(50, 50, "Red Text");
    
    setcolor(GREEN);
    outtextxy(50, 70, "Green Text");
    
    setcolor(BLUE);
    outtextxy(50, 90, "Blue Text");
    
    getch();
    closegraph();
    return 0;
}

2. settextstyle() Function

The settextstyle() function changes the font style, orientation, and size of text −

Syntax

void settextstyle(int font, int direction, int charsize);

Parameters:

  • font − Font type (DEFAULT_FONT, TRIPLEX_FONT, SMALL_FONT, etc.)
  • direction − Text direction (HORIZ_DIR or VERT_DIR)
  • charsize − Character size (1-10)

Example

#include <stdio.h>
#include <graphics.h>
#include <conio.h>

int main() {
    int gdriver = DETECT, gmode;
    initgraph(&gdriver, &gmode, "C:\TurboC3\BGI");
    
    settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4);
    outtextxy(50, 50, "Large Triplex Font");
    
    settextstyle(SMALL_FONT, HORIZ_DIR, 2);
    outtextxy(50, 100, "Small Font");
    
    getch();
    closegraph();
    return 0;
}

3. outtextxy() Function

The outtextxy() function displays text at specified coordinates −

Syntax

void outtextxy(int x, int y, char *textstring);

Complete Example: Colored Text with Different Fonts

#include <stdio.h>
#include <graphics.h>
#include <conio.h>

int main() {
    int gdriver = DETECT, gmode;
    initgraph(&gdriver, &gmode, "C:\TurboC3\BGI");
    
    /* Set background color */
    setbkcolor(WHITE);
    cleardevice();
    
    /* Display title in large red text */
    setcolor(RED);
    settextstyle(TRIPLEX_FONT, HORIZ_DIR, 5);
    outtextxy(100, 50, "Welcome!");
    
    /* Display subtitle in blue */
    setcolor(BLUE);
    settextstyle(DEFAULT_FONT, HORIZ_DIR, 3);
    outtextxy(150, 120, "TutorialsPoint");
    
    /* Display message in green */
    setcolor(GREEN);
    settextstyle(SMALL_FONT, HORIZ_DIR, 2);
    outtextxy(200, 180, "Learn C Programming");
    
    getch();
    closegraph();
    return 0;
}

Additional Text Functions

  • textheight() − Returns the height of a text string in pixels
  • textwidth() − Returns the width of a text string in pixels
  • outtext() − Displays text at current position
  • setbkcolor() − Sets background color

Common Color Constants

Color Name Value
BLACK 0
BLUE 1
GREEN 2
RED 4
WHITE 15

Conclusion

The graphics.h library provides powerful functions to create colorful and styled text output in C. These functions are essential for developing graphics-based applications and games with custom text formatting.

Updated on: 2026-03-15T12:33:58+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements