← Back to C Course | Chapter 13: Graphics | Lesson 1 of 10

C Graphics Introduction

What is Graphics in C?

Computer screens operate in two modes: text mode and graphics mode. Standard console programs use text mode. Graphics mode allows you to draw shapes, lines, and custom colors by controlling individual pixels on the screen. Traditional C graphics uses BGI (Borland Graphics Interface) via the graphics.h library, which is classic for learning computer graphics basics.

Example: What is Graphics in C?

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	putpixel(100, 100, WHITE);
	getch();
	closegraph();
	return 0;
}

The Graphics Coordinate System

In C graphics, the screen is a grid of pixels. The top-left corner is the origin point (0, 0). The X-coordinate increases to the right. The Y-coordinate increases downwards. This is different from normal cartesian coordinates where Y goes up.

Example: The Graphics Coordinate System

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	putpixel(0, 0, WHITE);
	getch();
	closegraph();
	return 0;
}

Compilation Requirements

To run graphics.h code on modern systems, you need an emulator like DOSBox or a compatible setup like Turbo C++. Alternatively, you can use modern libraries like winbgim or SDL2 which can compile under modern GCC configurations.

Example: Compilation Requirements

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	getch();
	closegraph();
	return 0;
}

Switching Back to Text Mode

When your drawing processes are complete, you must shut down the graphics system. The closegraph() function unloads the graphics drivers and restores the console back to standard text mode.

Example: Switching Back to Text Mode

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	putpixel(50, 50, WHITE);
	getch();
	closegraph();
	return 0;
}

Basic Graphic Concepts

The fundamental unit of a digital screen is the pixel. In graphics mode, you can change the state of individual pixels or draw complex shapes composed of many pixels. A basic understanding of coordinates and screen buffers helps in building fluid graphics output.

Example: Basic Graphic Concepts

c
#include <graphics.h>
int main() {
	initgraph(0, 0, "");
	putpixel(10, 10, WHITE);
	getch();
	closegraph();
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.