Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C Programming Code Examples

C > Games and Graphics Code Examples

3D graphics in C

/* 3D graphics in C */ #include<graphics.h> #include<dos.h> #include<stdio.h> void mainscreenin17408(int sx,int sy,int ex,int ey); void mainscreenin27408(int sx,int sy,int ex,int ey); void dia7408(); void main() { int gd=DETECT,gm; int sx,sy,ex,ey; initgraph(&gd,&gm,"e:\tc\bgi"); sx=15; sy=10; ex=630; ey=470; mainscreenin17408(sx,sy,ex,ey); mainscreenin27408(sx,sy,ex,ey); dia7408(); getch(); } void mainscreenin17408(int sx,int sy,int ex,int ey) { setfillstyle(1,7); bar(sx,sy,ex,ey); setfillstyle(1,7); setcolor(15); line(sx,sy,sx,ey); line(sx,sy,ex,sy); line(sx,sy,sx,ey); line(sx,sy,ex,sy); setcolor(0); line(ex,sy,ex,ey); line(ex,sy,ex,ey); line(sx,ey,ex,ey); line(sx,ey,ex,ey); settextstyle(0,0,0); setfillstyle(1,1); bar(sx+2,sy+2,ex-2,sy+20); setcolor(13); settextstyle(8,0,1); outtextxy(sx+190,sy-5,"IC 7408-AND GATE"); //for x setfillstyle(1,7); bar(ex-15,sy+4,ex-4,sy+15); setcolor(15); line(ex-15,sy+4,ex-4,sy+4); line(ex-15,sy+4,ex-15,sy+15); setcolor(0); line(ex-15,sy+15,ex-4,sy+15); line(ex-4,sy+4,ex-4,sy+15); setcolor(1); settextstyle(0,0,1); outtextxy(ex-13,sy+5,"x"); setfillstyle(1,7); } void mainscreenin27408(int sx,int sy,int ex,int ey) { sx=sx+15; sy=sy+24; ex=ex-17; ey=ey-9; setcolor(0); line(sx,sy,sx,ey); line(sx,sy,ex,sy); line(sx,sy,sx,ey); line(sx,sy,ex,sy); setcolor(15); line(ex,sy,ex,ey); line(ex,sy,ex,ey); line(sx,ey,ex,ey); line(sx,ey,ex,ey); } void dia7408() { int x1,y1,x2,y2,x3,y3,x4,y4,x5,y5,x6,y6,x7,y7,i; setcolor(0); rectangle(61,83,410,259);//body setfillstyle(1,9); floodfill(107,137,0); arc(62,166,270,90,16); setfillstyle(1,6); floodfill(68,165,0); //for drawing leads x1=62; y1=67; x2=84; y2=83; x3=73; y3=73; for(i=0;i<8;i++)//for upper part { rectangle(x1,y1,x2,y2); x1+=46; x2+=46; setfillstyle(1,14); floodfill(x3,y3,0); x3+=46; } x1=62; y1=259; x2=84; y2=275; x3=72; y3=267; for(i=0;i<8;i++)//for lower part { rectangle(x1,y1,x2,y2); x1+=46; x2+=46; setfillstyle(1,14); floodfill(x3,y3,0); x3+=46; } //end of ic's pin //start of ic internal configuration outtextxy(70,264,"1"); outtextxy(65,289,"A1"); outtextxy(116,264,"2"); outtextxy(112,289,"B1"); outtextxy(163,264,"3"); outtextxy(161,289,"Y1"); outtextxy(207,264,"4"); outtextxy(205,289,"A2"); outtextxy(253,264,"5"); outtextxy(250,289,"B2"); outtextxy(299,264,"6"); outtextxy(297,289,"Y2"); outtextxy(343,264,"7"); outtextxy(340,289,"GND"); outtextxy(390,264,"8"); outtextxy(387,289,"Y"); outtextxy(65,74,"16"); outtextxy(62,55,"Vcc"); outtextxy(111,74,"15"); outtextxy(112,54,"A4"); outtextxy(158,74,"14"); outtextxy(159,54,"B4"); outtextxy(204,74,"13"); outtextxy(205,54,"Y4"); outtextxy(250,74,"12"); outtextxy(248,54,"A3"); outtextxy(295,74,"11"); outtextxy(297,54,"B3"); outtextxy(340,74,"10"); outtextxy(337,54,"Y3"); outtextxy(394,74,"9"); outtextxy(387,54,"bv"); settextstyle(1,0,4); setlinestyle(0,0,3); setcolor(15); line(440,60,599,60); line(440,60,440,220); setcolor(0); line(440,220,599,220); line(599,220,599,60); setlinestyle(1,0,1); setfillstyle(1,0); setcolor(0); bar(447,63,592,90); settextstyle(1,0,1); setcolor(14); outtextxy(460,68,"RESULTS"); setcolor(2); for(y1=102,i=0;i<4;i++) { circle(462,y1,10); setfillstyle(1,4); floodfill(462,y1,2); y1+=30; } setlinestyle(0,0,1); setcolor(15); line(120,400,190,400); line(120,400,120,450); setcolor(0); line(120,450,190,450); line(190,450,190,400); setcolor(0); outtextxy(130,410,"Index"); setcolor(15); line(250,400,320,400); line(250,400,250,450); setcolor(0); line(250,450,320,450); line(320,450,320,400); outtextxy(260,410,"Exit"); settextstyle(1,0,3); setcolor(RED); outtextxy(150,320,"PIN DIAGRAM"); setlinestyle(0,0,3); line(150,350,297,350); setlinestyle(0,0,3); setcolor(15); line(420,270,600,270); line(420,270,420,440); setcolor(0); line(420,440,600,440); line(600,440,600,270); bar(422,272,596,310); setcolor(14); outtextxy(425,280,"Truth Table"); setcolor(MAGENTA); setlinestyle(0,0,1); line(430,320,590,320); line(430,320,430,430); line(430,430,590,430); line(590,430,590,320); line(530,320,530,430); setcolor(BROWN); settextstyle(0,0,1); outtextxy(460,330,"INPUTS"); outtextxy(540,330,"OUTPUT"); setcolor(MAGENTA); line(430,345,590,345);//for line after input and output line(480,345,480,430);//line for middle setcolor(12); outtextxy(450,355,"L"); outtextxy(500,355,"L"); outtextxy(560,355,"L"); outtextxy(450,375,"L"); outtextxy(500,375,"H"); outtextxy(560,375,"L"); outtextxy(450,395,"H"); outtextxy(500,395,"L"); outtextxy(560,395,"L"); outtextxy(450,415,"H"); outtextxy(500,415,"H"); outtextxy(560,415,"H"); setcolor(MAGENTA); line(430,370,590,370); line(430,390,590,390); line(430,410,590,410); line(430,430,590,430); }

Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language: Simple assignment operator. Assigns values from right side operands to left side operand. Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.

bar() function is a C graphics function that is used to draw graphics in the C programming language. The graphics.h header contains functions that work for drawing graphics. The bar() function is also defined in the header file. The bar() function is used to draw a bar ( of bar graph) which is a 2-dimensional figure. It is filled rectangular figure. The function takes four arguments that are the coordinates of (X, Y) coordinates of the top-left corner of the bar {left and top } and (X, Y) coordinates of the bottom-right corner of the bar {right and bottom}.

setlinestyle() is a function which is used to draw the line of different- different styles. Turbo C compiler provides five line styles that are solid, dotted, center, dashed and user defined. These all five line styles are already enumerated in graphics.h header file as given below: setlinestyle() function contains three parameters type, pattern and thickness.

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.

Settextstyle function is used to change the way in which text appears, using it we can modify the size of text, change direction of text and change the font of text. settextstyle sets the text font, the direction in which text is displayed, and the size of the characters. A call to settextstyle affects all text output by outtext and outtextxy.

initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver.

This library function is declared in graphics.h and used to draw a circle; it takes centre point coordinates and radius. Circle function is used to draw a circle with center (x,y) and third parameter specifies the radius of the circle. The code given below draws a circle. Where, (x, y) is center of the circle. 'radius' is the Radius of the circle.

floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled,border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle. floodfill fills an enclosed area on bitmap devices. (x,y) is a "seed point" within the enclosed area to be filled. The area bounded by the color border is flooded with the current fill pattern and fill color.

The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance. The for-loop statement is a very specialized while loop, which increases the readability of a program. It is frequently used to traverse the data structures like the array and linked list.

The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.

In C, the "main" function is treated the same as every function, it has a return type (and in some cases accepts inputs via parameters). The only difference is that the main function is "called" by the operating system when the user runs the program. Thus the main function is always the first code executed when a program starts. main() function is a user defined, body of the function is defined by the programmer or we can say main() is programmer/user implemented function, whose prototype is predefined in the compiler. Hence we can say that main() in c programming is user defined as well as predefined because it's prototype is predefined. main() is a system (compiler) declared function whose defined by the user, which is invoked automatically by the operating system when program is being executed.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program. Here are the two types of file that can be included using #include:

rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.

setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.

line() is a library function of graphics.c in c programming language which is used to draw a line from two coordinates. line() function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line.

In the C programming language, there is an option to create an arc of a circle of a given radius with a given center coordinates and degree of the arc. The arc() function is used to create an arc. This arc function is included in graphics.h library in C which contains methods that can draw figures on the output screen. The function to make an arc(), accepts five parameters for x, y co-ordinate, starting angle, end angle and radius. This will make the arc will all the values are fine. The Example below takes care of all these things as it have four arcs implemented.

outtextxy displays a text string in the viewport at the given position (x, y), using the current justification settings and the current font, direction, and size. To maintain code compatibility when using several fonts, use textwidth and textheight to determine the dimensions of the string. If a string is printed with the default font using outtext or outtextxy, any part of the string that extends outside the current viewport is truncated. outtextxy is for use in graphics mode; it will not work in text mode.




C language program coding to take the two integers as input and store it in the variables m & n respectively. Call function swap. Pass addresses of variables to the function swap.

This c language program takes any year as input and prints its last two digits. Take any year as input. Divide the input by 100 and obtain its remainder. The remainder got is

The above approach to list Even Numbers is not optimal. It unnecessarily iterates for odd numbers which is a performance issue and to overcome this start the loop with first even...


In C program user is asked to enter the binary number and the program then converts that binary number to the octal number by calling a user defined function. To understand this...

C program code shall help you learn one of basics of arrays. Copying an array involves index-by-index copying. For this to work we shall know the length of array in advance,...


The 'smallest element' is exchanged with the first element of the unsorted list of elements. The "second smallest element" is Exchanged with the 'second element' of the unsorted list

C programming code to read elements in an array and find frequency of each element in an array. C Program to count the occurrence of each element in an array. Logic to count...