How to Use initgraph() Function in C Programming Language

initgraph() Function in C Programming Language

Introduction :

Using graphics programming, you can bring your imagination to life on a digital canvas or screen.
In C or C++ programming language, the initgraph() function is generally used in graphics programming to initialize the graphics mode on the screen. This function is included in the graphics.h library which used to set up the environment for drawing graphics on the screen.

In this article, I shall explore you how to use initgraph() function using C or C++ graphics programming language.

What is initgraph() Function :

The initgraph() function is a very important in C and C++ graphics programming language. This function is commonly used in Turbo C++ IDE. It initializes the graphics mode and configuring the graphics driver for drawing graphics on the screen. It is commonly used for simple 2D graphics in C or C++ programming language. It is a function of the graphics.h library of Turbo C++ IDE.

The initgraph() functions are not part of the standard C or C++ library. These functions are specific to certain compilers like Turbo C++. After calling the initgraph() function and successfully initializes the graphics mode, you can use various graphics function for drawing lines, circles, rectangles, etc.

syntax of initgraph() function :

The following code is the syntax of initgraph() function.

The initgraph() function does not return any value. The initgraph() function takes three parameters or arguments such as pointer to graphic_driver, graphic_mode and BGI path. The first argument is a pointer to graphic driver variable which determines how graphics are displayed on the screen. You can use the DETECT constant to detect the appropriate driver automatically. There are different types of drivers also available such as DETECT, VGA, CGA, EGA etc.

The second argument is a pointer to graphics mode which defines the resolution, color depth and other display features for the graphics window. There are different types of graphics modes we can see such as CGA, EGA, VGA, HERC etc. The third argument is a path to BGI file. You have to give the correct BGI path depending on your system setup.

About the program :

This is a basic program to create a simple graphical window using C or C++ programming language. It is a simple example of using the initgraph() function to initialize the graphics system and set the background color of the graphics window. When you run the program on your pc, you can see a graphical window screen with a light yellow background. After pressing any key, the graphics window or screen will be closed.

Explanation of the program :

At beginning, you have to include the graphics.h and conio.h library in the program. The graphics.h library is used for graphical functions and conio.h for console input output functions. In the main() function, declare two integer variables such as “graphic_driver” and “graphic_mode”. The “graphic_driver” variable used to store the graphics driver. Here, it is initialized with the DETECT constant which detects the appropriate graphics driver automatically. The “graphic_mode” variable used to store the graphics mode.

After that, you can call the initgraph() function to initialize the graphics system. Using the specified graphics driver and mode, initgraph() function sets up the graphics environment. This function takes three parameters or arguments such as the address of “graphic_driver”, the address of “graphic_mode” and the path to the BGI (Borland Graphics Interface) file. Here, I use the BGI path “//turboc3/bgi” in the program. This BGI path may be different depending upon the configuration of your pc.

The default background color of the graphics window is black. To understand the program, I use the setbkcolor() function to change the background color of the graphics window. To see the program properly, I use the getch() function which takes a key press from you before close the program. Finally, using closegraph() function close the graphics system and release the resources used by the graphics system.

How run the program :

To run the program on your pc, first install the Turbo C++ IDE. After that, open the Turbo C++ IDE and create a C or C++ file with .c or .cpp extension. Now, copy the below code and paste in your C or C++ file of the Turbo C++ IDE. You have to know how to copy paste in the Turbo C++ IDE. Do you know how to use graphics.h in Turbo C++ IDE? Now run the program on your pc to see how to use initgraph() function using C or C++ programming language.

Source Code for initgraph() function :

The following code is used for initgraph() function in C or C++ graphics programming language.

Output of initgraph() function :

When you run the above program on your pc, you can see the below image as output.

output of initgraph() Function in C Programming Language

Conclusion :

After reading the above article, you can create graphical programs on your pc. Now, you can use other graphics function to create any type of graphics program as you like. Thank you for visiting my site.

Scroll to Top