C Program: Cyclic Redundancy Check (CRC)


A cycle redundancy check (CRC) in C programming is an error detecting commonly used in storage devices, etc. Block of data is entered and is checked and it is based on if the remainder is 0 or not and if it not found to be zero then an error is detected in the code. CRCs are popular because they are simple to implement in binary hardware. CRCs basically are used as xor operation is performed between two numbers if remainder is zero then no error if it’s not zero then error is detected.

We start our code by including the header files “stdio.h” and “conio.h”. Then we input the first number (dividend) in array n[] usually the larger number and in this example a 8 digit number Which is “10100001” and then the second number in array div[] if entered usually a smaller than the first number (divisor) in this example a 4 digit “1001”, after input of both the numbers few zeros(0) are added at the end of the first array(n[]) and the number of zeros are equal to the number of digits in divisor-1. For example if digits in divisor are 4 then 3 zeros will be added at the end of dividend.

Then after this a for loop is run until the number of digits of the first number (n[]) and then the first digit of the array n[] is checked if it is equal to 0 then a 0 gets added to the quotient but if it is equal to 1 then it enters another loop which checks or better xor the digits of the two numbers one by one, if they are equal to 1 then at the place of that digit a zero gets added in the first array (n[]).

And if the digits of the two numbers are not equal then at the place of that digit 1 gets added in the first array (n[]).After execution of both the loops the final value of the quotient is displayed on the output screen and the value of the remainder is also displayed which is being saved in array ( f[] ).

Variable “temp” is used instead of “i” to avoid confusion as the value of I has to be changed in the second for loop.

For this program I have already given a fixed input of fixed number of digits to be entered but you can ask the user too. The program is really simple you just need to understand the logic.

c-program-cyclic-redundancy-check-1 c-program-cyclic-redundancy-check-2 c-program-cyclic-redundancy-check-3

C Program: Cyclic Redundancy Check OutPut



Please enter your comment!
Please enter your name here