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

A TSR CLOCK

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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/* A TSR CLOCK */ #include "dos.h" void interrupt ( *prevtimer )( ) ; void interrupt mytimer( ) ; int running = 0 ; unsigned long far *time = ( unsigned long far * ) 0x46C ; char far *scr ; char far *mode ; main( ) { /* peek into location 0:410h and determine video mode */ if ( ( *mode & 0x30 ) == 0x30 ) scr = ( char far * ) 0xB0000000 ; else scr = ( char far * ) 0xB8000000 ; prevtimer = getvect ( 8 ) ; setvect ( 8, mytimer ) ; keep ( 0, 1000 ) ; } void interrupt mytimer( ) { unsigned char hours, sec, min ; if ( running == 0 ) { running = 1; hours = ( *time / 65520 ) ; min = ( *time - hours * 65520 ) / 1092 ; sec = ( *time - hours * 65520 - min * 1092 ) * 10 / 182 ; if ( sec >= 60 ) { sec -= 60 ; min++ ; if ( min == 60 ) { min = 0 ; hours++ ; if ( hours == 24 ) hours = 0 ; } } /* display digital clock */ writechar ( 48 + hours / 10, 0, 72, 112 ) ; writechar ( 48 + hours % 10, 0, 73, 112 ) ; writechar ( ':', 0, 74, 112 ) ; writechar ( 48 + min / 10, 0, 75, 112 ) ; writechar ( 48 + min % 10, 0, 76, 112 ) ; writechar ( ':', 0, 77, 112 ) ; writechar ( 48 + sec / 10, 0, 78, 112 ) ; writechar ( 48 + sec % 10, 0, 79, 112 ) ; running = 0 ; } ( *prevtimer )( ) ; } writechar ( char ch, int row, int col, int attr ) { *( scr + row * 160 + col * 2 ) = ch ; *( scr + row * 160 + col * 2 + 1 ) = attr ; }
Assignment Operators in C
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.
-=
Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.
*=
Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
/=
Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.
%=
Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.
<<=
Left shift AND assignment operator.
>>=
Right shift AND assignment operator.
&=
Bitwise AND assignment operator.
^=
Bitwise exclusive OR and assignment operator.
|=
Bitwise inclusive OR and assignment operator.
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
/* assignment operators in C language */ #include <stdio.h> main() { int a = 23; int c ; c = a; printf("Line 1 - = Operator Example, Value of c = %d\n", c ); c += a; printf("Line 2 - += Operator Example, Value of c = %d\n", c ); c -= a; printf("Line 3 - -= Operator Example, Value of c = %d\n", c ); c *= a; printf("Line 4 - *= Operator Example, Value of c = %d\n", c ); c /= a; printf("Line 5 - /= Operator Example, Value of c = %d\n", c ); c = 120; c %= a; printf("Line 6 - %= Operator Example, Value of c = %d\n", c ); c <<= 2; printf("Line 7 - <<= Operator Example, Value of c = %d\n", c ); c >>= 2; printf("Line 8 - >>= Operator Example, Value of c = %d\n", c ); c &= 2; printf("Line 9 - &= Operator Example, Value of c = %d\n", c ); c ^= 2; printf("Line 10 - ^= Operator Example, Value of c = %d\n", c ); c |= 2; printf("Line 11 - |= Operator Example, Value of c = %d\n", c ); }
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; }
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; }
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; }


Display the content from the same file you are writing the source code. Open the file you are currently writing using statement fopen and assign it to the pointer fp. Scan the every
Create menu driven Calculator that performs basic mathematic operations (Add, Subtract, Multiply, Divide) use switch case & functions. The calculator should input two numbers and
C language programming code to calculate sum of array. This program should give an insight of how to parse (read) array. We shall use a loop and sum up all values of the array.
C Programming to assignment operators. An Assignment Operator is used for assigning a value to a variable. C code demonstrate the working of assignment operators. The most
C Language Program which will accept string from the user. Pass this string to the function. Calculate the 'Length of String' using pointer. gets() is used to accept string with spaces. Str
C program to 'toggle case' of each characters of a string using loop. Logic to toggle case of a 'given string'. Input string from user, store it in some variable say str. Run a loop from start