In this tutorial ,you will code your first c graphics program.
start your turbo c/c++ compiler.
including the necessary header files:
you have to include necesary header files such as stdio.h,conio.h,and importantly graphics.h
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
Note
1 graphics.h header file is necessary for any c graphics program .dont forget to include it,otherwise the program just wont work.
2 .turbo c/c++ can be used in graphics mode as well as text mode,default is text mode.so,first you have to initiate graphics using "initgraph" function.
In this program let us try to draw a circle on the screen using circle() function.
Main function
int main()
{
int gd=DETECT,gm; ......................................line 1
clrscr(); .........................................................line 2
initgraph(&gd,&gm,"C:\\TC\\BGI");................line 3
circle(20,40,60);............................................line 4
getch();..........................................................line 5
return 0;.........................................................line 6
}
line 1: we have declared two integer type variables. i.e gd(graphics driver) and gm(graphic mode). gd is equated to macro DETECT which automatically selects the appropriate driver.gm is just an ordinary int type variable.
line 2: clrscr(); is used to clear the console screen .
line 3: initgraph() is used to initialize graphics .we are passing the address of gd,gm and the bgi folder.
line 4: circle function is called to draw a circle on the screen.
line 5: getch() is used to collect any character
line 6: zero is returned at the end of program.
Note:
if graphics mode is not properly initialized,you may get "graphics not initialized error". follow these steps carefully to initialize graphics correctly.
1.make sure that you have typed the macro "DETECT" in uppercase. c is case sensitive.
2."C:\\TC\\BGI" is the default address of BGI folder , if you have installed TC in any other folde r, make sure that you give the right address.and the address must be in UPPERCASE.
3. circle function takes 3 parameters.first two parameters are x and y co-ordinates and the third one is the radius parameter of the circle.
circle(int x,int y,int radius);
Complete Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
circle(10,20,30);
getch();
}
{
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
circle(10,20,30);
getch();
}
copy and save the program as yourfilename.c and compile and then run it.
Friends If You Enjoyed This Post Please Take 5 Seconds To Share It.