How to convert binary in C language to decimal?
Follow the following steps to use C language to convert binary numbers into decimal numbers:
Recommended: "C Language Tutorial"
1. First, set the function Sum in the main function, and define an array array[8] to store the input eight-digit binary number.
#2. Then use a for loop statement to input an eight-digit binary number. In the scanf function, add a 1 between %d, then use the printf function to output, and call the Sum function with the array name as an actual parameter.
#3. In the Sum function, a power function is declared, where n is the exponent, and term is the number converted to decimal for each digit, such as 1 *(2*2), sum is the total.
4. Then use a for loop statement, i is used to control the number of loops, n starts from the highest index 7 and decreases by one each time it loops, if the index is greater than or equal to zero , perform a loop, use n as an actual parameter when calling the power function, and finally return the sum.
#5. In the power function, if b is equal to zero, that is, the index is zero, then 1 is returned, otherwise it enters a loop, and j is used to control the number of loops.
#6. Finally, look at the running results. This C language program can successfully convert binary numbers into decimal numbers.
For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!
The above is the detailed content of How to convert binary to decimal in C language. For more information, please follow other related articles on the PHP Chinese website!