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 > Code Snippets Code Examples

Adds text to current dislay line while person is typing and increases

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* Adds text to current dislay line while person is typing and increases the cursor position */ #include<dos.h> void interrupt our(); void interrupt (*prev)(); void writechar(char ch,int row,int col,int attr); int a,b,kp,run=0; char far *scr; void main() { scr=(char far*) 0xb8000000; prev=getvect(9); setvect(9,our); keep(0,500); } void interrupt our() { kp++; if(kp==5) { run++; _AH=3; _BH=0; geninterrupt(0x10); a=_DH; b=_DL; if(run==1) writechar('M',a,b,7); if(run==2) writechar('S',a,b,7); if(run==3) { writechar('P',a,b,7); run=0; } b++; _AH=2; _BH=0; _DH=a; _DL=b; geninterrupt(0x10); kp=0; } (*prev)(); } void writechar(char ch,int row,int col,int attr) { *(scr+row*160+col*2)=ch; *(scr+row*160+col*2+1)=attr; }
If Else Statement in C
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.
Syntax for if-else Statement in C
if (test expression) { // run code if test expression is true } else { // run code if test expression is false }
If the test expression is evaluated to true, • statements inside the body of if are executed. • statements inside the body of else are skipped from execution. If the test expression is evaluated to false, • statements inside the body of else are executed • statements inside the body of if are skipped from execution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* if else statement in C language */ // Check whether an integer is odd or even #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); // True if the remainder is 0 if (number%2 == 0) { printf("%d is an even integer.",number); } else { printf("%d is an odd integer.",number); } return 0; }
geninterrupt() Function in C
dos.h header file contain geninterrupt() function which is used to create interrupt. geninterrupt(0x21) is used to generate 0x21 interrupt. Note that the sentence is ended with a '$' which is a terminating character. geninterrupt(0x21) means that we want to generate the 0x21 interrupt. The '0x' prefix is given which tells that the number is in hexadecimal. There are many interrupts, each having its own unique number.
Syntax for geninterrupt() Function in C
#include<dos.h> void geninterrupt(intr_num);
intr_num
Interrupt number The return value depends on the interrupt that was called. geninterrupt() is a macro. Interrupts are messages to the Pentium chip to halt it current activity, and perform our requested job. We can almost do anything using interrupts without using functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/* generate 0x21 interrupt by geninterrupt() function code example */ #include <conio.h> #include <dos.h> /* function prototype */ void writechar(char ch); int main(void) { clrscr(); gotoxy(80,25); writechar('*'); getch(); return 0; } /* outputs a character at the current cursor position using the video BIOS to avoid the scrolling of the screen when writing to location (80,25). */ void writechar(char ch) { struct text_info ti; /* grab current text settings */ gettextinfo(&ti); /* interrupt 0x10 sub-function 9 */ _AH = 9; /* character to be output */ _AL = ch; _BH = 0; /* video page */ _BL = ti.attribute; /* video attribute */ _CX = 1; /* repetition factor */ geninterrupt(0x10); /* output the char */ }
getvect() Function in C
getvect() reads the value of one of the MS-DOS "hard-wired" interrupt vectors. These vectors are numbered 0 to 255. The 4-byte value in each one is actually an address, which is the location of an interrupt function. The vector named in 'intr_num' is interpreted as a far pointer to some interrupt function.
Syntax for getvect() Function in C
#include<dos.h> void interrupt(*getvect(intr_num))();
intr_num
specific vector getvect() gets the four-byte address that is the interrupt vector for one of the interrupts (0 to 255) built into MS-DOS. The vector named in 'intr_num' is interpreted as a far pointer to the corresponding interrupt function. Function returns the 4-byte value representing the address of the interrupt function pointed to by the interrupt vector in 'intr-num'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* get the four-byte address by getvect() function code example */ #include <stdio.h> #include <dos.h> void interrupt get_out(); /* interrupt prototype */ void interrupt (*oldfunc)(); /* interrupt function pointer */ int looping = 1; int main(void) { puts("Press <Shift><Prt Sc> to terminate"); /* save the old interrupt */ oldfunc = getvect(5); /* install interrupt handler */ setvect(5,get_out); /* do nothing */ while (looping); /* restore to original interrupt routine */ setvect(5,oldfunc); puts("Success"); return 0; } void interrupt get_out() { looping = 0; /* change global variable to get out of loop */ }
setvect() Function in C
setvect() sets an interrupt vector entry. MS-DOS includes a set of hard-wired interrupt vectors. They are numbered 0 - 255. The 4-byte value in each vector is actually an address, which is the location of an interrupt function. setvect() sets the value of the vector 'intr_num' to a new value, 'vector'. 'vector' is a far pointer containing the address of a new interrupt function. The address of a C routine may only be passed to 'vector' if that routine is declared to be an 'interrupt' routine. The function, setvect(), takes two arguments.
Syntax for setvect() Function in C
#include<dos.h> void setvect(intr_num, vector);
intr_num
The first argument is an integer which is an interrupt number.
vector
The second argument is the name of a function which will serve as the interrupt handler. There is no return value. setvect() sets the value (an address) of the interrupt vector entry 'intr_num' to a new value specified by 'vector'. 'vector' is a far pointer that contains the address of a new interrupt function. A C routine must be declared an 'interrupt' routine for its address to be.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* set an interrupt vector entry bu setvect() function code example */ /* This is an interrupt service routine. You can NOT compile this program with test Stack Overflow turned on and get an executable file that will operate correctly.*/ #include <stdio.h> #include <dos.h> #include <conio.h> #define INTR 0X1C /* The clock tick interrupt */ #ifdef __cplusplus #define __CPPARGS ... #else #define __CPPARGS #endif void interrupt ( *oldhandler)(__CPPARGS); int count=0; void interrupt handler(__CPPARGS) { /* increase the global counter */ count++; /* call the old routine */ oldhandler(); } int main(void) { /* save the old interrupt vector */ oldhandler = getvect(INTR); /* install the new interrupt handler */ setvect(INTR, handler); /* loop until the counter exceeds 20 */ while (count < 20) printf("count is %d\n",count); /* reset the old interrupt handler */ setvect(INTR, oldhandler); return 0; }
main() Function in C
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. Its first function or entry point of the program from where program start executed, program's execution starts from the main. So main is an important function in c , c++ programming language.
Syntax for main() Function in C
void main() { ......... // codes start from here ......... }
void
is a keyword in C language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return any value. In place of void we can also use int return type of main() function, at that time main() return integer type value.
main
is a name of function which is predefined function in C library. • An operating system always calls the main() function when a programmers or users execute their programming code. • It is responsible for starting and ends of the program. • It is a universally accepted keyword in programming language and cannot change its meaning and name. • A main() function is a user-defined function in C that means we can pass parameters to the main() function according to the requirement of a program. • A main() function is used to invoke the programming code at the run time, not at the compile time of a program. • A main() function is followed by opening and closing parenthesis brackets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* basic c program by main() function example */ #include <stdio.h> #include <conio.h> main() { printf (" It is a main() function "); int fun2(); // jump to void fun1() function printf ("\n Finally exit from the main() function. "); } void fun1() { printf (" It is a second function. "); printf (" Exit from the void fun1() function. "); } int fun2() { void fun1(); // jump to the int fun1() function printf (" It is a third function. "); printf (" Exit from the int fun2() function. "); return 0; }
#include Directive in C
#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: • Header File or Standard files: This is a file which contains C/C++ function declarations and macro definitions to be shared between several source files. Functions like the printf(), scanf(), cout, cin and various other input-output or other standard functions are contained within different header files. So to utilise those functions, the users need to import a few header files which define the required functions. • User-defined files: These files resembles the header files, except for the fact that they are written and defined by the user itself. This saves the user from writing a particular function multiple times. Once a user-defined file is written, it can be imported anywhere in the program using the #include preprocessor.
Syntax for #include Directive in C
#include "user-defined_file"
Including using " ": When using the double quotes(" "), the preprocessor access the current directory in which the source "header_file" is located. This type is mainly used to access any header files of the user's program or user-defined files.
#include <header_file>
Including using <>: While importing file using angular brackets(<>), the the preprocessor uses a predetermined directory path to access the file. It is mainly used to access system header files located in the standard system directories.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* #include directive tells the preprocessor to insert the contents of another file into the source code at the point where the #include directive is found. */ // C program to illustrate file inclusion // <> used to import system header file #include <stdio.h> // " " used to import user-defined file #include "process.h" // main function int main() { // add function defined in process.h add(10, 20); // mult function defined in process.h multiply(10, 20); // printf defined in stdio.h printf("Process completed"); return 0; }


C program code Vertical Histogram. Define Maximum length of a word. Define Maximum No of words in a sentence. Excluding the space in the word length. Resetting the word
In Programming a Variable is a place holder for some value. All variables have some type associated with them, which expresses what 'type' of values they can be assigned. C offers
Graphic program about Mine Sweeper. Put mines in random blocks. Put mines value in other blocks. Sets view port to the play area only. Puts F tag in Array. Determine current
Choose a two digit number and add digits. Subtract the sum from original number. See the symbol opposit to the number you got. Press any key The MIND READER will display
Your time has started. Start typing. Hit Esc when done. Erase previous character instead of cursoring. Eliminate a special character that is printed for Esc. A Backspace followed
C code binary numbers pyramid pattern. First and Third Line is Starting with 1, While 2nd & 4th Line is starting with 0, So for First & Third Line count will be 1 and for even Line Number