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

C Program to Convert Octal to Binary with Example

C Program to Convert Octal to Binary with Example

Converting numbers between different bases is a common task in programming. Say you have an octal number, 17. How do you find its binary equivalent using C? This program walks you through it step by step. Soon, you'll be able to convert any octal number to binary and know exactly how it works.

Prerequisites for Octal to Binary Conversion in C

Make sure you have these:

  • Understanding of the % and / operators.
  • Familiarity with while loops and if conditions.
  • <stdio.h> for standard input and output.
  • 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 Octal to Binary

Here's the complete code that converts an octal number to its binary equivalent.

octal_to_binary.cc▶ Run
#include <stdio.h>

void decimal_to_binary(int decimal) {
    int binary[32], i = 0;
    while (decimal > 0) {
        binary[i] = decimal % 2;
        decimal /= 2;
        i++;
    }
    for (int j = i - 1; j >= 0; j--)
        printf("%d", binary[j]);
    printf("\n");
}

int main() {
    int octal, decimal = 0, base = 1;
    printf("Enter an octal number: ");
    scanf("%d", &octal);

    while (octal != 0) {
        int rem = octal % 10;
        decimal += rem * base;
        base *= 8;
        octal /= 10;
    }

    printf("Binary equivalent: ");
    decimal_to_binary(decimal);

    return 0;
}

Try this code yourself — free

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

▶ Open Compiler

Output of the Convert Octal to Binary Program

The following is the expected output when you run the program.

Outputtext
Enter an octal number: 17
Binary equivalent: 1111
Output of C Program to Convert Octal to Binary
Output of C Program to Convert Octal to Binary

Algorithm to Convert Octal to Binary in C

Converting an octal number to binary involves two main steps: converting octal to decimal, followed by decimal to binary. This is the step-by-step logic the program uses.

  1. Read the octal number from the user.
  2. Initialize decimal = 0 and base = 1.
  3. Check if the octal number is not zero.
  4. Extract the last digit using rem = octal % 10.
  5. Add rem * base to decimal.
  6. Multiply base by 8.
  7. Remove the last digit by performing octal /= 10.
  8. Repeat steps 3-7 until all digits are processed.
  9. Convert the decimal to binary.
flowchart TD
  A([Start]) --> B[/Read octal number/]
  B --> C[initialize binary = 0]
  C --> D[initialize base = 1]
  D --> E{"octal != 0?"}
  E -->|Yes| F[rem = octal %% 10]
  F --> G[decimal = decimal + rem * base]
  G --> H[base = base * 8]
  H --> I[octal = octal / 10]
  I --> E
  E -->|No| J[call decimal_to_binary(decimal)]
  J --> K([End])
Flowchart: the algorithm step by step

How the C Program to Convert Octal to Binary Works

Let's break down how this program converts octal to binary.

  • #include <stdio.h>: Includes the standard library for input and output functions.
  • int main(): The main function where execution starts.
  • Declare variables octal, decimal, and base: decimal and base initialize to zero and one, respectively.
  • printf and scanf are used to prompt and read the octal number from the user.
  • The while loop runs as long as the octal is not zero, extracting each digit using % and /.
  • The remainder rem is multiplied by base and added to decimal.
  • base is updated to the next power of 8.
  • Once all digits are processed, decimal_to_binary is called to convert and print the binary equivalent.
  • return 0: Terminates the program successfully.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

Why do we multiply by 8 in the octal to decimal conversion process?

🎉Correct! In octal, each digit represents a power of 8, so we multiply to adjust place value.
🤔Not quite. Think about how place value works in octal numbers.

Time and Space Complexity of the C Program to Convert Octal to Binary

Here's a look at the efficiency of this program.

Time Complexity: O(n). Here, n is the number of digits in the octal number you input. For example, if you input 17, n is 2. The while loop in the main function iterates over each digit of the octal number, and the decimal to binary conversion also processes each part individually. So, 2 digits mean 2 iterations in each conversion phase.

Space Complexity: O(1). The program uses a fixed amount of memory for variables like octal, decimal, and base, which do not grow with the size of the input.

⚠️

Common mistake: Forgetting to update the base when converting octal to decimal will result in incorrect calculations.

Key takeaway: Converting between number systems like octal and binary often involves some intermediate steps to decimal.

FAQs About C Program to Convert Octal to Binary

Here are some common questions about converting octal numbers to binary using C.

Convert each octal digit to its 3-digit binary equivalent, then concatenate them.
Decimal acts as a middleman, letting for accurate base conversion.
Yeah, but the storage type for decimal might limit how big.
printf and scanf from <stdio.h> are used for input and output.
It keeps dividing the decimal number by 2, writing down remainders as binary digits.
To store binary digits for reverse order printing, since remainders are gotten in reverse.

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

Conclusion

Coding conversions between number systems can be pretty fascinating. The logic here is super handy for formatting outputs, error checking, or even data compression tasks. If you've got an interest in number systems, try exploring hexadecimal conversions next. It builds on the same principles but with a few new twists.

Practice more with the C quiz or run this program in the EduSeekho online C compiler again for a deeper understanding.