Errors frequently occur in data when it is written to a disk, transmitted across a network or otherwise manipulated. The errors are typically very small, for example, a single incorrect bit, but even such small errors can greatly affect the quality of data, and even make it useless.
Among the types of errors that cannot be detected by simple checksum algorithms are reordering of the bytes, inserting or deleting zero-valued bytes and multiple errors that cancel each other out. Fortunately, however, these errors can be detected with more sophisticated methods, such as cyclic redundancy checks (CRC). Although such techniques have the disadvantage of requiring greater system resources (in the form of processor time and bandwidth), this has become an increasingly unimportant consideration in recent years as a result of the continued increases in processor speed and bandwidth.
Here is source code of the C Program to Implement the Checksum Method for Small String Messages and Detect If the Received message is same as the Transmitted. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
#include<stdio.h>
#include<conio.h>
int add(int, int);
int com(int);
void main() {
int i, j, dl, dil;
int data1[10], data2[10], newdata[10], comp[10], checksum[10];
printf("\n Enter the data length=");
scanf("%d", &dl);
printf("\n Enter the data1 : \n");
for (i = 0; i < dl; i++)
scanf("%d", &data1[i]);
printf("\n Enter the data2 : \n");
for (i = 0; i < dl; i++)
scanf("%d", &data2[i]);
for (i = dl - 1; i >= 0; i--) {
newdata[i] = add(data1[i], data2[i]);
}
printf("\n\n Data 1 : ");
for (i = 0; i < dl; i++)
printf("%d", data1[i]);
printf("\n Data 2 : ");
for (i = 0; i < dl; i++)
printf("%d", data2[i]);
printf("\n\n The new data is : ");
for (i = 0; i < dl; i++) {
printf("%d", newdata[i]);
}
printf("\n Checksum : ");
for (i = 0; i < dl; i++) {
checksum[i] = com(newdata[i]);
printf("%d", checksum[i]);
}
printf("\n\n Receiver Side : \n");
printf("\n Data : ");
for (i = 0; i < dl; i++)
printf("%d", data1[i]);
printf(" ");
for (i = 0; i < dl; i++)
printf("%d", data2[i]);
printf(" ");
for (i = 0; i < dl; i++)
printf("%d", checksum[i]);
printf("\n Addition : ");
for (i = dl - 1; i >= 0; i--) {
newdata[i] = add(newdata[i], checksum[i]);
}
for (i = 0; i < dl; i++) {
printf("%d", newdata[i]);
}
printf("\n Compliment : ");
for (i = 0; i < dl; i++) {
comp[i] = com(newdata[i]);
printf("%d", comp[i]);
}
}
int add(int x, int y) {
static int carry = 0;
if (x == 1 && y == 1 && carry == 0) {
carry = 1;
return 0;
} else if (x == 1 && y == 1 && carry == 1) {
carry = 1;
return 1;
} else if (x == 1 && y == 0 && carry == 0) {
carry = 0;
return 1;
} else if (x == 1 && y == 0 && carry == 1) {
carry = 1;
return 0;
} else if (x == 0 && y == 1 && carry == 0) {
carry = 0;
return 1;
} else if (x == 0 && y == 1 && carry == 1) {
carry = 1;
return 0;
} else if (x == 0 && y == 0 && carry == 0) {
carry = 0;
return 0;
} else {
carry = 0;
return 1;
}
}
int com(int a) {
if (a == 0)
return 1;
else
return 0;
}
Output:
$ gcc Checksum.c $ ./a.out Enter the data length=4 Enter the data1 : 1253 Enter the data2 : 7564 Sender Side: Data 1 : 1253756422934004199878 Data 2 : 2293336420172342047964194432 The new data is : 1111 Checksum : 0000 Receiver Side : Data : 1253756422934004199878 2293336420172342047964194432 0000 Addition : 1111 Compliment : 0000
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Buy Computer Science Books
- Watch Advanced C Programming Videos
- Apply for C Internship