Checksum Program in C

Checksum Program in C: Checksum is an error detection mechanism that detects the error in the data/message in a data communication from sender to receiver. It is only an error detection mechanism and hence cannot correct the errors in the message bits.

It uses Checksum generator on the sender side and Checksum checker on the receiver side to check the errors.

Example:
Number of messages transmitted: 2
Elements to calculate Checksum:
1011
1110

****SENDER SIDE****
SUM IS: 2121
CHECKSUM IS: 2122

****RECEIVER SIDE****
SUM IS: 2121
CHECKSUM IS: 0

Problem Description

Write a C Program to to implement checksum.

Problem Solution

1. Ask the user for number of messages transmitted say N.
2. Ask for N messages from the user.
3. Calculate the SUM and CHECKSUM at Sender Side.
4. Calculate the SUM and CHECKSUM at Receiver Side.
5. Exit.

advertisement
advertisement
Program/Source Code

Here is the source code of the C program to implement Checksum. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. /*
  3.  * C program to implement Checksum
  4.  */
  5.  
  6. #include<stdio.h>
  7. #include<math.h>
  8.  
  9. int sender(int arr[10],int n)
  10. {
  11.     int checksum,sum=0,i;
  12.     printf("\n****SENDER SIDE****\n");
  13.     for(i=0;i<n;i++)
  14.     sum+=arr[i];
  15.     printf("SUM IS: %d",sum);
  16.     checksum=~sum;    //1's complement of sum
  17.     printf("\nCHECKSUM IS:%d",checksum);
  18.     return checksum;
  19. }
  20.  
  21. void receiver(int arr[10],int n,int sch)
  22. {
  23.     int checksum,sum=0,i;
  24.     printf("\n\n****RECEIVER SIDE****\n");
  25.     for(i=0;i<n;i++)
  26.         sum+=arr[i];
  27.     printf("SUM IS:%d",sum);
  28.     sum=sum+sch;
  29.     checksum=~sum;    //1's complement of sum
  30.     printf("\nCHECKSUM IS:%d",checksum);
  31. }
  32.  
  33. void main()
  34. {
  35.     int n,sch,rch;
  36.     printf("\nENTER SIZE OF THE STRING:");
  37.     scanf("%d",&n);
  38.     int arr[n];
  39.     printf("ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:\n");
  40.     for(int i=0;i<n;i++)
  41.     {
  42.         scanf("%d",&arr[i]);
  43.     }
  44.     sch=sender(arr,n);
  45.     receiver(arr,n,sch);
  46. }
Program Explanation

1. Ask the user to enter size of the string n.
2. Initialize an array of size n arr[n].
3. Enter n elements into the array to calculate Checksum.
4. To calculate Checksum at sender side pass the array and number of elements to a function sender() and store the value returned by the function into a variable sch.
5. Inside the sender function run a loop from i=0 to n and in each iteration calculate sum as sum = sum + arr[i]. It will result in addition of all the elements in the array.
6. Now, calculate checksum as 1’s complement of the variable sum and print both sum and checksum for the sender side.
7. Return the value of Checksum and store it in a variable sch.
8. To calculate Checksum at receiver side pass the array, number of elements and the also the sch to a function receiver().
9. Inside the receiver function run a loop from i=0 to n and in each iteration calculate sum as sum = sum + arr[i]. It will result in addition of all the elements in the array.
10. Now, add the value of sum with the value of sch and store it in the variable sum. It is done in order to decode the checksum generated by the sender.
11. calculate checksum as 1’s complement of the variable sum and print both sum and checksum for the receiver side.

Example:
  • Consider, the size of string is n=2 and the two elements are 1011 and 0111. These two values will be stored in arr[] and will be passed to sender function.
  • Inside the sender function run a loop and calculate the sum of both the elements i.e., sum = 1122. Take the 1’s of sum to calculate checksum i.e., checksum = -1123.
  • Now, print sum and checksum at the sender side and return the value of checksum and store it in the variable sch.
  • At the receiver function pass the arr[], n, sch. Inside the function run a loop and calculate the sum of both the elements then come out of the loop and print sum at sender side i.e., sum = 1122.
  • Now add the value of sch to the value of sum and finally store it in the same variable sum to decode the checksum generated at the sender side.
  • Take the 1’s complement of sum and store it in the variable checksum i.e., checksum = 0. Now, print the value of checksum for the receiver side.

Time Complexity: O(n)
The above program for generating checksum has a time complexity of O(n), as the for loop runs from 0 to n, where n is the number of elements in the array.

Note: Join free Sanfoundry classes at Telegram or Youtube

Space Complexity: O(n)
In the above program, space complexity is O(n) as an array of size n has been initialized to store the values in it.

Runtime Test Cases

Testcase 1: In this case, we enter “2” as the length of the string and “1011” and “0111” as array elements to calculate the checksum.

ENTER SIZE OF THE STRING:2
ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:
1011
0111
 
****SENDER SIDE****
SUM IS: 1122
CHECKSUM IS:1123
 
****RECEIVER SIDE****
SUM IS:1122
CHECKSUM IS:0

Testcase 2: In this case, we enter “4” as the length of the string and “10110101”, “11000111”, “01101100” and “00001111” as array elements to calculate the checksum.

ENTER SIZE OF THE STRING:4
ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:
10110101
11000111
01101100
00001111
 
****SENDER SIDE****
SUM IS: 22212423
CHECKSUM IS:22212424
 
****RECEIVER SIDE****
SUM IS:22212423
CHECKSUM IS:0
Another Approach: C Program to find checksum

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.

advertisement

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.

Program/Source Code

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. {
  7.     int i, j, dl, dil;
  8.     int data1[10], data2[10], newdata[10], comp[10], checksum[10];
  9.     printf("\n Enter the data length=");
  10.     scanf("%d", &dl);
  11.     printf("\n Enter the data1 : \n");
  12.     for (i = 0; i < dl; i++)
  13.         scanf("%d", &data1[i]);
  14.     printf("\n Enter the data2 : \n");
  15.     for (i = 0; i < dl; i++)
  16.         scanf("%d", &data2[i]);
  17.     for (i = dl - 1; i >= 0; i--)
  18.     {
  19.         newdata[i] = add(data1[i], data2[i]);
  20.     }
  21.  
  22.     printf("\n\n Data 1        : ");
  23.     for (i = 0; i < dl; i++)
  24.         printf("%d", data1[i]);
  25.     printf("\n Data 2        : ");
  26.     for (i = 0; i < dl; i++)
  27.         printf("%d", data2[i]);
  28.  
  29.     printf("\n\n The new data is : ");
  30.     for (i = 0; i < dl; i++)
  31.     {
  32.         printf("%d", newdata[i]);
  33.     }
  34.     printf("\n Checksum : ");
  35.     for (i = 0; i < dl; i++)
  36.     {
  37.         checksum[i] = com(newdata[i]);
  38.         printf("%d", checksum[i]);
  39.     }
  40.  
  41.     printf("\n\n Receiver Side : \n");
  42.     printf("\n Data : ");
  43.     for (i = 0; i < dl; i++)
  44.         printf("%d", data1[i]);
  45.     printf(" ");
  46.     for (i = 0; i < dl; i++)
  47.         printf("%d", data2[i]);
  48.     printf(" ");
  49.     for (i = 0; i < dl; i++)
  50.         printf("%d", checksum[i]);
  51.  
  52.     printf("\n Addition : ");
  53.     for (i = dl - 1; i >= 0; i--)
  54.     {
  55.         newdata[i] = add(newdata[i], checksum[i]);
  56.     }
  57.     for (i = 0; i < dl; i++)
  58.     {
  59.         printf("%d", newdata[i]);
  60.     }
  61.     printf("\n  Compliment : ");
  62.     for (i = 0; i < dl; i++)
  63.     {
  64.         comp[i] = com(newdata[i]);
  65.         printf("%d", comp[i]);
  66.     }
  67. }
  68.  
  69. int add(int x, int y)
  70. {
  71.     static int carry = 0;
  72.     if (x == 1 && y == 1 && carry == 0)
  73.     {
  74.         carry = 1;
  75.         return 0;
  76.     }
  77.     else if (x == 1 && y == 1 && carry == 1)
  78.     {
  79.         carry = 1;
  80.         return 1;
  81.     }
  82.     else if (x == 1 && y == 0 && carry == 0)
  83.    {
  84.         carry = 0;
  85.         return 1;
  86.     }
  87.     else if (x == 1 && y == 0 && carry == 1)
  88.     {
  89.         carry = 1;
  90.         return 0;
  91.     }
  92.     else if (x == 0 && y == 1 && carry == 0)
  93.     {
  94.         carry = 0;
  95.         return 1;
  96.     }
  97.     else if (x == 0 && y == 1 && carry == 1)
  98.     {
  99.         carry = 1;
  100.         return 0;
  101.     }
  102.     else if (x == 0 && y == 0 && carry == 0)
  103.     {
  104.         carry = 0;
  105.         return 0;
  106.     }
  107.     else
  108.     {
  109.         carry = 0;
  110.         return 1;
  111.     }
  112. }
  113. int com(int a)
  114. {
  115.     if (a == 0)
  116.         return 1;
  117.     else
  118.         return 0;
  119. }
Runtime Test Cases

In this case, we enter “4” as the length of the data and “1253” and “7564” as array elements to calculate the checksum.

advertisement
 
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

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.