ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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


      8 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 

      8 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 

      8 years ago

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


    • profile image


      8 years ago

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

      plzzzzzzz reply soon

    • brumboy profile image


      8 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


      9 years ago

      nice job dude

    • profile image


      9 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 imageAUTHOR

      Akhil Anil 

      9 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


      9 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


      9 years ago

      its very useful to me .thank u

    • profile image


      9 years ago

      how u display an image??

    • profile image


      9 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 imageAUTHOR

      Akhil Anil 

      9 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 imageAUTHOR

      Akhil Anil 

      9 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 

      10 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


      10 years ago

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

    • nanospeck profile imageAUTHOR

      Akhil Anil 

      10 years ago

      cheeeers sparkle!

    • profile image


      10 years ago

      Love your effort!

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


    • NoRR4Me profile image


      10 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 imageAUTHOR

      Akhil Anil 

      10 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


      10 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.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)