✓ Link copied to clipboard
C 📅 July 9, 2026 ⏱ 5 min read

C Program to Convert Binary to Decimal with Code and Output

C Program to Convert Binary to Decimal with Code and Output

Binary numbers are the basis of computing. But if you're working with humans, decimals are often easier to understand. So, converting binary to decimal in C is a handy skill. For instance, the binary 1101 converts to decimal 13. Let’s see how you can write a program to make this conversion effortlessly.

Prerequisites for the C Program to Convert Binary to Decimal

Before diving into the code, ensure you understand these concepts:

  • Character arrays and strings in C.
  • Using loops for iteration.
  • The printf() and scanf() functions.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Convert Binary to Decimal

Here's the complete code to convert a binary number to its decimal equivalent:

binary_to_decimal.cC▶ Run
#include <stdio.h>

int main() {
    char binary[65];
    int i = 0, decimal = 0;

    // Read binary number from user
    printf("Enter a binary number: ");
    scanf("%s", binary);

    // Convert binary to decimal
    while (binary[i] != '\0') {
        decimal = decimal * 2 + (binary[i] - '0');
        i++;
    }

    // Output the decimal value
    printf("The decimal value is: %d\n", decimal);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Convert Binary to Decimal Program

When you run the program and input a binary number, it outputs the decimal equivalent. Here's an example:

Outputtext
Enter a binary number: 1101
The decimal value is: 13
Output of C Program to Convert Binary to Decimal
Output of C Program to Convert Binary to Decimal

Algorithm to Convert Binary to Decimal in C

This C program reads a binary number and converts it into a decimal by processing each bit from left to right. Here's how it works step by step:

  1. Read the binary number as a string from the user.
  2. Initialize two variables: i for index and decimal for storing the result.
  3. Loop through each bit in the string until the null terminator is reached.
  4. Convert the current binary digit to decimal and update the decimal variable.
  5. Print the decimal equivalent.
flowchart TD
  A([Start]) --> B[/Read binary number as a string/]
  B --> C[i = 0, decimal = 0]
  C --> D{binary[i] != '\0'?}
  D -->|Yes| E["decimal = decimal * 2 + (binary[i] - '0')"]
  E --> F[i++]
  F --> D
  D -->|No| G[/Print decimal/]
  G --> H([End])
Flowchart: the algorithm step by step

How the C Program to Convert Binary to Decimal Works

Let's break down the code to understand how the conversion happens:

  • #include <stdio.h>: This header file is required for input and output functions, such as printf and scanf.
  • char binary[65]: Declares an array to store the binary input, assuming a maximum of 64 bits plus the null terminator.
  • int i = 0, decimal = 0: Initializes the index for the character array and a variable to store the resulting decimal number.
  • scanf("%s", binary): Reads the binary number from the user as a string.
  • while (binary[i] != '\0'): Loops through each character in the string until the end.
  • decimal = decimal * 2 + (binary[i] - '0'): Calculates the decimal value by shifting left (multiplying by 2) and adding the current bit's value.
  • i++: Moves to the next character in the binary string.
  • printf("The decimal value is: %d\n", decimal): Outputs the calculated decimal value.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the decimal representation of binary 101?

🎉Correct! The binary 101 equals 5 in decimal.
🤔Not quite. Think about how binary to decimal conversion works.

How Fast Is the Convert Binary to Decimal Program in C?

Let's discuss how efficient this conversion process is:

Time Complexity: O(n). The loop goes through each bit of the binary number exactly once, checking each character and updating the decimal value. For a binary number with n digits — like 1101, which has 4 digits — it means 4 iterations.

Space Complexity: O(1). The program uses the same few variables: binary, i, and decimal. Their number doesn't change with the size of the input.

⚠️

Common mistake: Don’t forget to convert binary characters from '0' and '1' to integer values using binary[i] - '0'.

Key takeaway: This program converts binary numbers to decimal efficiently using simple arithmetic, a fundamental operation in computer science.

FAQs About C Program to Convert Binary to Decimal

Here are answers to common questions about this conversion program:

Converting binary to decimal means multiplying each bit by 2 raised to its positional index and summing them. This program does that using a loop.
It's for handling larger binary numbers and includes a null terminator.
Nope, the program assumes valid binary input. You'd need extra checks if you want to handle invalid input.
Input over 64 bits will cause overflow and give incorrect results.
The conversion is efficient with a time complexity of O(n), where n is the number of bits.

If you found this program helpful, check out these related programs:

Conclusion

Binary to decimal conversion is important in many applications. Knowing this can set you up for more complex tasks with number systems. This code is a good jumping-off point for diving into binary arithmetic.

Practice your skills further with the C language intermediate quiz or run this code in the EduSeekho compiler.