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

A Simple 2D-Drawing Program

/* A Simple 2D-Drawing Program */ #include<stdio.h> #include<conio.h> #include<graphics.h> #define ESC 0x1b void drcir(int,int,int); void drarc(int,int,int); void main() { int a,b,c,d,e,f,curcolor=3; int gdriver = DETECT,gmode; initgraph(&gdriver,&gmode,"c:\tc\bgi"); verybegin: sound(333); delay(100); sound(222); delay(100); sound(250); delay(100); sound(33); delay(100); sound(111); delay(100); sound(222); delay(100); sound(333); delay(100); sound(3333); delay(100); nosound(); gotoxy(25,1); printf("2d Drawing"); gotoxy(18,5); printf("A Simple Program"); gotoxy(18,6); printf("This is used for drawing circles,arcs,rectangle,"); gotoxy(18,7); printf("lines and many more..."); printf(" The Controls For The Two-Dimensional Drawing "); printf(" N(Up Arrow) = North "); printf(" S(Down Arrow) = South "); printf(" E(Right Arrow) = East "); printf(" W(Left Arrow) = West "); printf(" F(Page Up) = North-East "); printf(" X(Home) = North-West "); printf(" D(Page Down) = South-East "); printf(" Z(End) = South-West "); printf(" J = Current Position "); printf(" B = Help(This Screen) "); printf(" R(Insert) = Reset "); printf(" T = Color Change(0-9) "); printf(" C = Circle "); printf(" A = Arc "); printf(" ESC = Quit "); getch(); begin: curcolor=3; a = getmaxx() / 2; b = getmaxy() / 2; cleardevice(); start: putpixel(a,b,curcolor); c=getch(); sound(100); delay(20); nosound(); if(c==ESC) { goto end; } switch(c) { case 'N': case 'n': case 'H': case 'h': c=a; d=b-1; a=c; b=d; goto start; case 'S': case 's': case 'P': case 'p': c=a; d=b+1; a=c; b=d; goto start; case 'E': case 'e': case 'M': case 'm': c=a+1; d=b; a=c; b=d; goto start; case 'W': case 'w': case 'K': case 'k': c=a-1; d=b; a=c; b=d; goto start; case 'D': case 'd': case 'Q': case 'q': c=a+1; d=b+1; a=c; b=d; goto start; case 'X': case 'x': case 'G': case 'g': c=a-1; d=b-1; a=c; b=d; goto start; case 'Z': case 'z': case 'O': case 'o': c=a-1; d=b+1; a=c; b=d; goto start; case 'F': case 'f': case 'I': case 'i': c=a+1; d=b-1; a=c; b=d; goto start; case 'R': case 'r': goto begin; case 'J': case 'j': f=curcolor+1; putpixel(a,b,f); gotoxy(1,2); printf("X=%d,Y=%d",a,b); sound(100); getch(); gotoxy(1,2); printf(" "); nosound(); goto start; case 'C': case 'c': drcir(a,b,curcolor); goto start; case 'A': case 'a': drarc(a,b,curcolor); goto start; case 'T': case 't': gotoxy(1,2); printf("Enter color code:"); e=getche(); curcolor=e; setcolor(curcolor); gotoxy(1,2); printf(" "); goto start; case 'B': case 'b': cleardevice(); goto verybegin; default: goto start; } end: cleardevice(); closegraph(); } void drcir(int x,int y,int ccolor) { int r; gotoxy(1,2); printf("Enter Radius of Circle:"); scanf("%d",&r); gotoxy(1,2); printf(" "); setcolor(ccolor); circle(x,y,r); } void drarc(int x,int y,int ccolor) { int r,sa,ea; gotoxy(1,2); printf("Enter Radius:"); scanf("%d",&r); gotoxy(1,2); printf(" "); gotoxy(1,2); printf("Enter Starting Angle:"); scanf("%d",&sa); gotoxy(1,2); printf(" "); gotoxy(1,2); printf("Enter Ending Angle:"); scanf("%d",&ea); gotoxy(1,2); printf(" "); setcolor(ccolor); arc(x,y,sa,ea,r); }

The header file graphics.h contains cleardevice() function. cleardevice() is a function which is used to clear the screen by filling the whole screen with the current background color. It means that cleardevice() function is used to clear the whole screen with the current background color and it also sets the current position to (0,0). . Both clrscr() and cleardevice() functions are used to clear the screen but clrscr() is used in text mode and cleardevice function is used in the graphics mode.

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.

A goto statement in C programming language provides an unconditional jump from the 'goto' to a labeled statement in the same function. The goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the program control to a predefined label. The goto statment can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can't be done by using a single break statement.

#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:

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition.

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.

The gotoxy() function places the cursor at the desired location on the screen. This means it is possible to change the cursor location on the screen using the gotoxy() function. It is basically used to print text wherever the cursor is moved.

In the C Programming Language, the #define directive allows the definition of macros within your source code. These macro definitions allow constant values to be declared for use throughout your code. Macro definitions are not variables and cannot be changed by your program code like variables. You generally use this syntax when creating constants that represent numbers, strings or expressions.

Our system can create various sounds on different frequencies. The sound() is very useful as it can create very nice music with the help of programming and our user can enjoy music during working in out the program. Sound function produces the sound of a specified frequency. Used for adding music to a C program, try to use some random values in loop, vary delay and enjoy.

The header file graphics.h contains getmaxx() function which returns the maximum X coordinate for current graphics mode and driver. getmaxx() returns the maximum (screen-relative) x value for the current graphics driver and mode. For example, on a CGA in 320*200 mode, getmaxx returns 319. getmaxx is invaluable for centering, determining the boundaries of a region onscreen, and so on.

The nosound() function in C language is used to stop the sound played by sound() function. nosound() function is simply silent the system. Sound function produces the sound of a specified frequency and nosound function turn off the PC speaker.

putpixel() plots a point in the color defined by color at (x,y). The header file graphics.h contains putpixel() function which plots a pixel at location (x, y) of specified color. Where, (x, y) is the location at which pixel is to be put, and color specifies the color of the pixel. To put a pixel on the screen at a particular position, calling the pixel() function is a good way. This function takes three parameters as the position of the pixel and also the color of the pixel.

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.

Read formatted data from stdin. Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string. In C programming, scanf() is one of the commonly used function to take input from the user. The scanf() function reads formatted input from the standard input such as keyboards. The scanf() function enables the programmer to accept formatted inputs to the application or production code. Moreover, by using this function, the users can provide dynamic input values to the application.

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.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found. If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.

Get the next keystroke from the console, and echo it. getche() function is a function in C programming language which waits for any character input from keyboard and it will also echo the input character on to the output screen. getche() function echoes the character to the screen whereas getch() does not do so. This is the only difference between both the functions. It can be remembered by the use of character 'e' at the end of getche() function.

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.

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.

The header file graphics.h contains getmaxy() function which returns the maximum Y coordinate for current graphics mode and driver. getmaxy returns the maximum (screen-relative) y value for the current graphics driver and mode. For example, on a CGA in 320*200 mode, getmaxy returns 199. getmaxy is invaluable for centering, determining the boundaries of a region onscreen, and so on.

Writes the C string pointed by format to the standard output (stdout). If format includes format specifiers (subsequences beginning with %), the additional arguments following format are formatted and inserted in the resulting string replacing their respective specifiers. printf format string refers to a control parameter used by a class of functions in the input/output libraries of C programming language. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data (such as a number) to characters. "printf" is the name of one of the main C output functions, and stands for "print formatted". printf format strings are complementary to scanf format strings, which provide formatted input (parsing). In both cases these provide simple functionality and fixed format compared to more sophisticated and flexible template engines or parsers,

Delay function is used to suspend execution of a program for a particular time. delay() function requires a parameter which should be a number, defining the milliseconds for the delay. To use delay function in your program you should include the "dos.h" header file which is not a part of standard C library. Here unsigned int is the number of milliseconds (remember 1 second = 1000 milliseconds).

The header file graphics.h contains closegraph() function which closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the mode it was in before you called initgraph. closegraph() function is used to re-enter in the text mode and exit from the graphics mode. If you want to use both text mode and graphics mode in the program then you have to use both initgraph() and closegraph() function in the program.

Program takes two strings as input & checks whether two strings are anagrams. Take two strings as input and store them in the arrays array1[] array2[] respectively. In the function

C program print all natural numbers from 1 to n using loop. Input upper limit to print natural number from user. Store it in a variable say N. Run a For Loop from 1 to N with 1 increment.

This C Program calculates the perimeter of a circle, rectangle and triangle. This program is used to find the perimeter of circle, rectangle and triangle. Perimeter of rectangle: 2 *(x+y)

C Programming Language code to convert binary code of a number into its equivalent gray's code without using recursion. Gray is also represented using 0s and 1s. Speciality

C Program Code to Convert the number of bytes into bits by multiplying the bytes with 8. Use two functions namely signed_one() and unsigned_one() for calculating the range