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 > Hardware Interaction Through C Code Examples

Program for implementing all the display functions

/* Program for implementing all the display functions */ #include<stdio.h> #include<conio.h> #include<dos.h> #include<process.h> #include<iostream.h> # include<ctype.h> main() { int choice; char cont='y'; int a1q1(),a1q2(),a1q3(),a1q4(),a1q5(),a1q6(),a1q7(),a1q8(),a1q9(),a1q10(); clrscr(); gotoxy(24,3); while(cont=='y') { clrscr(); printf(" ---------------------------------------"); printf(" |1.Get current Display Mode. | "); printf(" |2.Character with chosen attribute. | "); printf(" |3. Scroll the window up. | "); printf(" |4.Scroll the window down. | "); printf(" |5.Read the character & it's attribute| "); printf(" |6.Positioning the Cursor . | "); printf(" |7.Selecting the Video Mode. | "); printf(" |8.Selecting the type of the Cursor. | "); printf(" |9.Reading the Cursor position | "); printf(" |10.Select the page of the text. | "); printf(" |11. Exit. | "); printf(" ---------------------------------------"); printf(" Enter your choice [ ]"); gotoxy(37,16); scanf("%d",&choice); switch(choice) { case 1: a1q1(); break; case 2: a1q2(); break; case 3: a1q3(); break; case 4: a1q4(); break; case 5: a1q5(); break; case 6: a1q6(); break; case 7: a1q7(); break; case 8: a1q8(); break; case 9: a1q9(); break; case 10: a1q10(); break; case 11: exit(0); } gotoxy(3,21); printf(" Do you want to continue[y/n]: [ ]"); gotoxy(32,22); cin>>cont; } getch(); } a1q1() { clrscr(); union REGS regs; regs.h.ah=0x0f; int86(0x10,®s,®s); int noofcol; int displaymode; int activetextpage; noofcol = regs.h.ah; displaymode = regs.h.al; activetextpage = regs.h.bh; printf(" "); printf(" No. of Columns on Screen - %d ",noofcol); printf(" Display Mode - %d ",displaymode); printf(" The Active Text Page - %d ",activetextpage); return(0); } a1q2() { clrscr(); union REGS regs; regs.h.ah = 2; regs.h.dh = 0; regs.h.dl = 0; regs.h.bh = 0; int86(0x10,®s,®s); getch(); regs.h.ah = 9; regs.h.bh = 0; regs.h.al = 65; regs.h.dl = 8; regs.h.cl = 2; regs.h.ch = 0; int86(0x10,®s,®s); return(0); } a1q3() { clrscr(); union REGS regs; regs.h.ah = 6; regs.h.al = 5; regs.h.bh = 8; regs.h.ch = 0; regs.h.cl = 0; regs.h.dh = 50; regs.h.dl = 50; int86(0x10,®s,®s); return(0); } a1q4() { clrscr(); union REGS regs; regs.h.ah = 7; regs.h.al = 5; regs.h.bh = 8; regs.h.ch = 0; regs.h.cl = 0; regs.h.dh = 50; regs.h.dl = 50; int86(0x10,®s,®s); return(0); } a1q5() { clrscr(); int x,y; union REGS regs; regs.h.ah = 2; regs.h.dh = 0; regs.h.dl = 0; regs.h.bh = 0; int86(0x10,®s,®s); getch(); regs.h.ah = 8; regs.h.bh = 0; int86(0x10,®s,®s); x = regs.h.al; y = regs.h.ah; clrscr(); printf("Ascii Character is - %d ",x); printf("The attribute of Character is - %d ",y); return(0); } a1q6() { clrscr(); int x,y; union REGS regs; printf(" Enter the X-position - "); scanf("%d",&x); printf(" Enter the Y-position - "); scanf("%d",&y); regs.h.ah = 2; regs.h.bh = 0; regs.h.dh = y; regs.h.dl = x; int86(0x10,®s,®s); return(0); } a1q7() { clrscr(); int choice; union REGS regs; printf(" For CGA Mode(0-6)."); printf(" For Mono MOde(1)."); printf(" Select the Mode - "); scanf("%d",&choice); clrscr(); regs.h.ah = 0; /*Set cursor position*/ regs.h.al = choice; int86(0x10,®s,®s); gotoxy(40,12); printf(" HAVE A NICE DAY"); return(0); } a1q8() { clrscr(); int x,y; union REGS regs; printf(" Enter the starting line of the cursor(0-4) - "); scanf("%d",&x); printf(" Enter the ending line of the cursor - "); scanf("%d",&y); gotoxy(40,12); regs.h.ah = 1; regs.h.ch = x; regs.h.cl = y; int86(0x10,®s,®s); return(0); } a1q9() { clrscr(); union REGS regs; regs.h.ah = 3; regs.h.bh = 9; regs.h.dh = 8; regs.h.dl = 27; int86(0x10,®s,®s); printf(" The row position of cursor on selected page - %d",regs.h.ch); printf(" The column position of cursor on selected page - %d",regs.h.cl); return(0); } a1q10() { clrscr(); union REGS regs; regs.h.ah = 5; regs.h.bh = 6; int86(0x10,®s,®s); return(0); }

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.

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,

While loop is also known as a pre-tested loop. In general, a while loop allows a part of the code to be executed multiple times depending upon a given boolean condition. It can be viewed as a repeating if statement. The while loop is mostly used in the case where the number of iterations is not known in advance. The while loop evaluates the test expression inside the parentheses (). If test expression is true, statements inside the body of while loop are executed. Then, test expression is evaluated again. The process goes on until test expression is evaluated to false. If test expression is false, the loop terminates.

The exit() function is used to terminate a process or function calling immediately in the program. It means any open file or function belonging to the process is closed immediately as the exit() function occurred in the program. The exit() function is the standard library function of the C, which is defined in the stdlib.h header file. So, we can say it is the function that forcefully terminates the current program and transfers the control to the operating system to exit the program. The exit(0) function determines the program terminates without any error message, and then the exit(1) function determines the program forcefully terminates the execution process.

A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose. To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows:

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.

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:

Function clrscr() clears the screen and moves the cursor to the upper left-hand corner of the screen. If you are using the GCC compiler, use system function to execute the clear/cls command. clrscr() function is also a non-standard function defined in "conio.h" header. This function is used to clear the console screen. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

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.

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.

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.

The break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops.

C language program code print to Equilateral Triangle. Triangle with all Sides Equal is called Equilateral Triangle. We shall now see how to print stars *, in Equilateral Triangle shape and

C Language program function to convert one or more floating-point values to a string with the values separated by commas. C Function allocates memory that must be freed by the

Declare recursive function to find factorial of a number. First let us give a meaningful name to our function, say fact(). Factorial Function accepts an integer input whose factorial is to