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
Write a C Program to to implement checksum.
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.
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.
/*
* C program to implement Checksum
*/
#include<stdio.h>
#include<math.h>
int sender(int arr[10],int n)
{
int checksum,sum=0,i;
printf("\n****SENDER SIDE****\n");
for(i=0;i<n;i++)
sum+=arr[i];
printf("SUM IS: %d",sum);
checksum=~sum; //1's complement of sum
printf("\nCHECKSUM IS:%d",checksum);
return checksum;
}
void receiver(int arr[10],int n,int sch)
{
int checksum,sum=0,i;
printf("\n\n****RECEIVER SIDE****\n");
for(i=0;i<n;i++)
sum+=arr[i];
printf("SUM IS:%d",sum);
sum=sum+sch;
checksum=~sum; //1's complement of sum
printf("\nCHECKSUM IS:%d",checksum);
}
void main()
{
int n,sch,rch;
printf("\nENTER SIZE OF THE STRING:");
scanf("%d",&n);
int arr[n];
printf("ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
sch=sender(arr,n);
receiver(arr,n,sch);
}
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.
- 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.
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.
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
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.
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;
}
In this case, we enter “4” as the length of the data and “1253” and “7564” as array elements to calculate the checksum.
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”.
- Apply for C Internship
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Practice Computer Science MCQs