Checksum Program in C

«
»
This is a C Program to implement checksum method and verfiy message. A checksum is a simple type of redundancy check that is used to detect errors in data.

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.

advertisement

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.

  1. #include<stdio.h>
  2. #include<conio.h>
  3. int add(int, int);
  4. int com(int);
  5. void main() {
  6.     int i, j, dl, dil;
  7.     int data1[10], data2[10], newdata[10], comp[10], checksum[10];
  8.     printf("\n Enter the data length=");
  9.     scanf("%d", &dl);
  10.     printf("\n Enter the data1 : \n");
  11.     for (i = 0; i < dl; i++)
  12.         scanf("%d", &data1[i]);
  13.     printf("\n Enter the data2 : \n");
  14.     for (i = 0; i < dl; i++)
  15.         scanf("%d", &data2[i]);
  16.     for (i = dl - 1; i >= 0; i--) {
  17.         newdata[i] = add(data1[i], data2[i]);
  18.     }
  19.  
  20.     printf("\n\n Data 1        : ");
  21.     for (i = 0; i < dl; i++)
  22.         printf("%d", data1[i]);
  23.     printf("\n Data 2        : ");
  24.     for (i = 0; i < dl; i++)
  25.         printf("%d", data2[i]);
  26.  
  27.     printf("\n\n The new data is : ");
  28.     for (i = 0; i < dl; i++) {
  29.         printf("%d", newdata[i]);
  30.     }
  31.     printf("\n Checksum : ");
  32.     for (i = 0; i < dl; i++) {
  33.         checksum[i] = com(newdata[i]);
  34.         printf("%d", checksum[i]);
  35.     }
  36.  
  37.     printf("\n\n Receiver Side : \n");
  38.     printf("\n Data : ");
  39.     for (i = 0; i < dl; i++)
  40.         printf("%d", data1[i]);
  41.     printf(" ");
  42.     for (i = 0; i < dl; i++)
  43.         printf("%d", data2[i]);
  44.     printf(" ");
  45.     for (i = 0; i < dl; i++)
  46.         printf("%d", checksum[i]);
  47.  
  48.     printf("\n Addition : ");
  49.     for (i = dl - 1; i >= 0; i--) {
  50.         newdata[i] = add(newdata[i], checksum[i]);
  51.     }
  52.     for (i = 0; i < dl; i++) {
  53.         printf("%d", newdata[i]);
  54.     }
  55.     printf("\n  Compliment : ");
  56.     for (i = 0; i < dl; i++) {
  57.         comp[i] = com(newdata[i]);
  58.         printf("%d", comp[i]);
  59.     }
  60. }
  61.  
  62. int add(int x, int y) {
  63.     static int carry = 0;
  64.     if (x == 1 && y == 1 && carry == 0) {
  65.         carry = 1;
  66.         return 0;
  67.     } else if (x == 1 && y == 1 && carry == 1) {
  68.         carry = 1;
  69.         return 1;
  70.     } else if (x == 1 && y == 0 && carry == 0) {
  71.         carry = 0;
  72.         return 1;
  73.     } else if (x == 1 && y == 0 && carry == 1) {
  74.         carry = 1;
  75.         return 0;
  76.     } else if (x == 0 && y == 1 && carry == 0) {
  77.         carry = 0;
  78.         return 1;
  79.     } else if (x == 0 && y == 1 && carry == 1) {
  80.         carry = 1;
  81.         return 0;
  82.     } else if (x == 0 && y == 0 && carry == 0) {
  83.         carry = 0;
  84.         return 0;
  85.     } else {
  86.         carry = 0;
  87.         return 1;
  88.     }
  89. }
  90. int com(int a) {
  91.     if (a == 0)
  92.         return 1;
  93.     else
  94.         return 0;
  95. }

Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
$ 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.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.