Program - 3: AIM: Write A C Program To Implement Cyclic Redundant Check
Program - 3: AIM: Write A C Program To Implement Cyclic Redundant Check
PROGRAM -3
DESCRIPTION:
The cyclic redundancy check, or CRC, is a technique for detecting errors in digital data, but not
for making corrections when errors are detected. It is used primarily in data transmission. In the CRC
method, a certain number of check bits, often called a checksum, are appended to the message being
transmitted. The receiver can determine whether or not the check bits agree with the data, to ascertain
with a certain degree of probability whether or not an error occurred in transmission. If an error occurred,
the receiver sends a “negative acknowledgement” (NAK) back to the sender, requesting that the message
be retransmitted.
#include <stdio.h>
#include <stdlib.h>
void crcfunc(int[],int[]);
int len1,len,len2;
int res[30],l=0;
int main()
{
int m[30],g[10];
int i,j;
int rm[30];
printf("\nEnter the length of message:");
scanf("%d",&len1);
printf("\nEnter the message:");
for(i=0;i<len1;i++)
{
scanf("%d",&m[i]);
res[i]=m[i];
}
printf("\nEnter the length of generator:");
scanf("%d",&len2);
len=len1+len2-1;
printf("\nEnter the generator:");
for(i=0;i<len2;i++)
scanf("%d",&g[i]);
for(i=len1;i<len;i++)
m[i]=0;
crcfunc(m,g);
printf("\nMessage to be transmitted:");
for(i=0;i<len;i++)
printf("%d ",res[i]);
printf("\nEnter the received message");
for(i=0;i<len;i++)
scanf("%d",&rm[i]);
crcfunc(rm,g);
for(i=len1;i<len;i++)
{
if(res[i]==0)
l=1;
else
{
l=0;
break;
}
}
if(l==1)
printf("\nMessage received without errors");
else
printf("\nMessage received with errors");
return 0;
}
void crcfunc(int m[],int g[])
{
int tem[len1];
int i,j,c,k=0;
do
{
c=0;
for(j=0,i=k;j<len2&&i<(len);i++,j++)
{
m[i]=m[i]^g[j];
tem[j]=m[i];
}
for(j=0;j<(len1+len2-1);j++)
{
if(tem[j]==0)
{
k++;
c++;
}
else
break;
}
}while(c<=(len-i));
for(i=len,j=len;j>=len1;j--,i--)
res[j]=m[i];
printf("CRC -");
for(i=len1;i<len;i++)
printf("%d",res[i]);
}
OUTPUT 1:
CRC -1011
Message to be transmitted:1 0 1 0 1 1 0 1 1 0 1 0 1 0 1 1
CRC -0100
OUTPUT 2:
CRC -1110
Message to be transmitted:1 1 0 1 0 1 1 0 1 1 1 1 1 0
CRC -0000