ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

Borland C Graphics.h Tutorials | How Computer graphics using C graphics library in Programming

Updated on December 8, 2011

Borland C Graphics

If 70% of C programming is inheritance,structure,classes and other theoretical subjects the rest 40% is related with C graphics programming.It is the fun side of programming actually.I was Ignorant about graphics programming in c ,c++ since the day I learned #include<iostream.h> the portion is not usually covered in usual C programming tutorials , there are special books for C graphics programming alone.Nobody will speak about graphics if you go and search for 'learn C' in google.I still believe its necessary to teach about graphics programming with the basic lesson itself.There was nobody there to teach me about about this topic , finally I dug up some c graphics programming books from my college library and mastered the concepts in few days.Its simple than the rest of the OOPS concepts actually.So let's move into work.

Your Screen For C graphics

Borland C graphics.h tutorial

The most basic thing you have to learn before stepping in the C shore of graphics is about the monitor.

The monitor you are staring at right now contains lots of small dots called pixels , when each pixels glow in different colours you see an image.You can imagine it as minute bricks which can change to any colour used in building up the screen you see.

When we initialize a programming for using C graphics.h it converts the whole screen into a matrix, if you don’t know what a matrix in mathematics means ,just understand that your screens top left corner is taken as origin the x-axis increases by width from left to right and the y-axis increases by length from top to bottom.The co-ordinates we enter in each function is mapped according to this order.Now I don’t want to confuse you with further complicated theory.If you need to get indepth technichal knowledge about this topic you can go for C grapics book from amazon i've linked somewhere in the bottom of this page.We will hover straight into the application now.For developing graphics using C we need to do it in three simple steps.

  1. Loading the header file of C graphics.h
  2. Initializing 'graphics driver' and 'grahpics mode'
  3. Using the functions where ever necessary

Loading The Header File

void main()

Loading the header file of C graphics.h

The C graphics.h is the header file which should be included to initalize your computer to start using grahipcs methods and also to initialize the monitor.These kinds of statements before the main program are called preprocessor dierctives.Its very simple as coded below.

Initializing Graphics Form Borland C graphics library

This initialization is done inside the main program.The method initgraph()  is used for this purpose there are three parameters for this , the first two are of integer type and the next is the path which you can leave blank in quoutes to take the default path.The integer parameters are initalized in this method for all the C graphics programs so you just need to memorize it and apply it for all C graphics programming from now on.

Initializing 'Graphics Driver' and 'Graphics Mode' from Borland C graphics library

void main()
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode," ");

Methods In Borland C Graphics programming





rectangle(x,y,int x_width,int y_height)






drawpoly(num_of_points + 1, points)





setviewport(int left,int top,int right,int bottom, int clip)




Some Common C Graphics Programming Methods

Well, these are some of the most popular C Graphics Methods used in while graphics programming soon after you learn these you can think of developing small games , puzzles and so on.I'll just give only a small overview on each methods.x,y,x1,y1,x2,y2 etc are all variables you'll have to substitute with numeric values to get working programs.Further clear-cut reference can be obtained from any of the C graphics book available from amazon or your local library(those books would be pretty old and dusty by now). If you find it difficult to view the long C graphics library functions code in the window because the code is too lengthy, then click on the 'view plain' written in small fonts at the top left of the codes box on the left side of this text.


Clears all previous graphical outputs generated by the previous programs.It's a good practice to include this method at the starting of each program.


This will initialize the graphics cusor to the specified co-ordiante.In C gotoxy function is used very frequently to locate the cursor at different locations whenever as necessary.


It will colour the pixel specified by the co-ordinates.


This method is used to display a text in any position on the screen.The numeric coordinates are substituted for x and y.


Draws a rectangle according to the given parameter x and y are the top-left corner co-ordinates.


Draws a circle with x,y as the center .


Draws a line as per the given co-ordinates.


Cursor is moved from the current location to the specified location dx,dy.These parameters can also be used as incremental values.


Draws a line from its current location to the co-ordinate(x,y)


Draws the ellipse with the specified angles and coordinates.


Draws a polygon with (num_of_points +1) edges.The array 'points'

int points[ ]=(x1,y1,x2,y2,x3,y3...)



The direction can be changed as HORIZ_DIR or VERT_DIR

The character size increases from 1 to 10


The fill styles available are SOLID_FILL, LINE_FILL, HATCH_FILL, SLASH_FILL etc.


Sets the color


The function fills the enclosed area about x,y with the highest value of the color returned by the getmaxcolor().


This marks a rectangle starting from (10,10) having widht 630 pixels height 470 pixels.The integer specifies this identified area may be clipped. clearviewport() reverse this function.


Converts the integer x1into an alphabet, and puts it in st1, which is an array to hold 10 characters.10 is the buffer size.


Cause a pause in execution of the program 1000ms= 1 second


Terminates all graphics operations and revert the hardware back to the normal mode.

C Graphics Book

If you are really into mastering the C/C++ Graphics I would suggest you to go for only two books:

  • Graphics Programming in C++: Writing Graphics Applications for Windows 98

This is a very well written book on designing your own graphics applications for Windows, no matter what type of application you are wanting to do. Mark Walmsley goes into good detail about designing many different types of applications from simple display program to advanced subjects such as 3D modeling. Mr. Walmsley address many common errors and pitfalls with the included examples.I would recommend this book to anyone who is interested in writing a graphics application.

  • Advanced Graphics Programming Using C/C++

A book is a good source for advanced PC graphics topics currently being used in a wide variety of fields. Stresses a hands-on approach, providing numerous program examples written in C and applicable to any C compiler with correct, ready-to-use and well-described code. Covers ray tracing, used to create realistic 3-D graphics. Includes information on graphical file formats and manipulating digital images. Also focuses on printing screens and images.

Bonus Three Debugging C Tips

This is a small bonus tip from me about debugging C.When you start programming , first few times you would find that debugging is a nasty job but once you learn understand the heart of C everything will be so easy.These are the three most common error which a programmer (mostly novice) come across but yet the get confused in correcting it.

  • I always have a confusion while using the special characters for new line(\n) , tab space(\t) etc whether its /n or \n.To easily remember that keep " %"- this symbol in mind.Just type the slash in the opposite direction.
  • The most common errors while for a novice programmer is that he forgets to put the semicolon ";" at the end of a statement.
  • Another tricky error which frequently occurs is that you forget to put the "&" symbol while taking an input with scanf in 'C'.This wont generate any error messages but the you will get strange results as output. This is because all the arguments pointers pointing to the memory.The scanf function will not perform correctly because it will be attempting to overwrite the wrong sections of memory, rather than pointing to the memory location of the variable you are attempting to get input for.

Was This Article Helpful?

If this article was helpful for you, vote it up by clicking the beautiful green oval a few paragraph below. Your experience may be a lesson for others also. Do share your incredible insights about More Graphics Tutorials, Tips & Tricks For C on the comment box below.


    0 of 8192 characters used
    Post Comment

    • profile image

      ss005 5 years ago

      If I am correct, bcc5.5 does not have "graphics.h" header file unlike from tc3.0, then what is the alternative of "graphic.h" in bcc5.*?

    • Java Programs profile image

      Java Programs 5 years ago from India

      Highly impressed by the effort for writing this hub .... excellent tutorials ..... thanks for such a wonderful hub .... keep the good work up ..... thanks .....

    • profile image

      sem jen 5 years ago

      Really, appreciable efforts and a very handy material for the beginners for CG....


    • profile image

      bss 5 years ago

      how to change the origin frm topleft corner???????

      plzzzzzzz reply soon

    • brumboy profile image

      brumboy 6 years ago from UK

      It is also possible to link in the graphics library so that it is contained within the program itself. This can be found in the Borland C++ users guide.

      Saves the program from looking for a separate graphics.lib file!

    • profile image

      Abhishek 6 years ago

      nice job dude

    • profile image

      Karan 6 years ago


      How to get rid of BGI error. I tried running a program which shows a circle but Borland C++ 5.02 gives an error which says "BGI graphics not supported under Windows"

      Plz help

    • nanospeck profile image

      Akhil Anil 6 years ago

      Hi chiste.. i've mentioned that it's for the novice programmers. I didn't want to confuse them with pointers as this article was not meant only to be a graphics tutorial. However since you found it less explaining i've added the necessary details. Hope you find this article helpful!


    • profile image

      chiste 6 years ago

      "you will get strange results as output with no reason."

      if you don't understand how reference[&] works, you shouldn't call them "strange results"

    • profile image

      banu 6 years ago

      its very useful to me .thank u

    • profile image

      razor 7 years ago

      how u display an image??

    • profile image

      sushanta 7 years ago

      hi this help me a lot like i was waiting for this knowlage for last few months this site help me a lot i cant express my happynes through these few words

    • nanospeck profile image

      Akhil Anil 7 years ago

      @peter : Thanks for the suggestion peter. actually, this tutorial is an 'intermeddiate' level. I belive a person who is somewhat familiar with the programming language would find it easy to follow.. I also didn't want to reproduce things already available on the internet. still i'll try to improve. Thanks again.

    • nanospeck profile image

      Akhil Anil 7 years ago

      @vikas: i suggest you buy some books about it vikas, its difficult to find topic about graphcs over the internet. but still i would search over and let u know soon.

    • profile image

      Peter neck 7 years ago

      The source code for programs should be supplied in the manner that they could easily b understood by a dummy man.

    • profile image

      vikas 7 years ago

      is there any site for best tutorial for computer graphics programming in C++

    • nanospeck profile image

      Akhil Anil 7 years ago

      cheeeers sparkle!

    • profile image

      sparkle 7 years ago

      Love your effort!

      It has really encouraged me to add more graphics in to my c project.


    • NoRR4Me profile image

      NoRR4Me 7 years ago

      Oh ok. You wanted it to say 70% and 40% I got it now. lol.

      It's nice to share. Others will appreciate it.

      You're welcome. :-)

    • nanospeck profile image

      Akhil Anil 7 years ago

      Hi NoRR4Me, I just meant to apply a kind of figure of speech there ;).Graphics grow into Games, OS etc and forms too interactive when programmed perfectly.It exceeds what is 100% of C.Got Me??? Guess So.. Your comment is very encouraging NoRR. I wrote his hub with the main aim to help others coz i had to refer a bit to find about this myself.

      Thanks A LoT!!! :)

    • NoRR4Me profile image

      NoRR4Me 7 years ago

      Nanospeck, nice hub. In the first sentence you said 40% is graphic, but should it be 30% if the other is 70%? That would total 100%.

      I don't know anything about C programming, but you should get good traffic to this hub if no one else is helping on this subject and lots of people are searching for it. That is if the search engines rank your hub well. You have everything nicely written and illustrated.

      Best wishes.