Program in C to Convert Octal to Binary

Program in C to Convert Octal to Binary

Octal numbers use base-8, while binary numbers use base-2. Converting octal numbers to binary is useful in programming for compact representations of binary data and low-level system tasks. In this article, we will write a C program to convert an octal number into its binary equivalent.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

Understanding The Problem

Each octal digit corresponds exactly to three binary digits. To convert octal to binary, each digit is separately converted to its 3-bit binary equivalent, and these groups are combined to form the full binary number. For example, octal 57 converts to binary 101111.

Program 1: Using Arithmetic Method

In this approach, we first convert the octal number into decimal, and then convert the decimal number into binary.

#include <stdio.h>
#include <math.h>

int main() {

    int octal, decimal = 0, i = 0, rem;
    long long binary = 0, factor = 1;

    printf("Enter an octal number: ");
    scanf("%o", &octal);  // %o reads octal input

    // Convert octal to decimal
    int temp = octal;

    while (temp > 0) {

        rem = temp % 10;
        decimal += rem * pow(8, i);
        temp /= 10;
        i++;

    }

    // Convert decimal to binary
    temp = decimal;

    while (temp > 0) {

        rem = temp % 2;
        binary += rem * factor;
        temp /= 2;
        factor *= 10;

    }

    printf("Binary equivalent: %lld\n", binary);

    return 0;

}

This method first converts octal to decimal using powers of 8, then converts decimal to binary by repeatedly dividing by 2 and forming the binary number.

Program 2: Using Strings

This method treats the octal input as a string and converts each digit into a 3-bit binary group, avoiding decimal conversion and handling large numbers safely.

#include <stdio.h>
#include <string.h>

int main() {

    char octal[20];
    int i;

    printf("Enter an octal number: ");
    scanf("%s", octal);

    printf("Binary equivalent: ");

    for (i = 0; i < strlen(octal); i++) {

        switch (octal[i]) {

            case '0': printf("000"); break;
            case '1': printf("001"); break;
            case '2': printf("010"); break;
            case '3': printf("011"); break;
            case '4': printf("100"); break;
            case '5': printf("101"); break;
            case '6': printf("110"); break;
            case '7': printf("111"); break;
            default: printf("Invalid octal digit"); return 1;

        }

    }

    printf("\n");

    return 0;

}

This string-based method directly maps each octal digit to its binary equivalent, making it simple and safe for large inputs.

Program 3: Using sprintf() and Decimal Conversion

We can also use sprintf() to convert the decimal equivalent of the octal number into binary.

#include <stdio.h>
#include <math.h>

int main() {

    int octal, decimal = 0, i = 0, rem;
    char binary[64];

    printf("Enter an octal number: ");
    scanf("%o", &octal);  // Read octal input

    // Convert octal to decimal
    int temp = octal;

    while (temp > 0) {

        rem = temp % 10;
        decimal += rem * pow(8, i);
        temp /= 10;
        i++;

    }

    // Convert decimal to binary string using sprintf
    sprintf(binary, "%b", decimal); // C23 feature: %b prints binary

    printf("Binary equivalent: %s\n", binary);

    return 0;

}

This approach is concise and leverages C23’s %b format specifier for binary output, avoiding manual calculations.

Program 4: Using sscanf()

This method reads the octal input as a string, converts it to decimal using sscanf(), and then prints the binary equivalent using %b.

#include <stdio.h>

int main() {

    char octalStr[20];
    unsigned int decimal;

    printf("Enter an octal number: ");
    scanf("%s", octalStr);

    // Convert octal string to decimal using sscanf and %o
    sscanf(octalStr, "%o", &decimal);

    // Print binary using %b (C23)
    printf("Binary equivalent: %b\n", decimal);

    return 0;

}

In this program, sscanf() reads the octal string and automatically converts it into a decimal integer. Then %b in printf() outputs the number in binary. This method is concise, safe for large inputs, and avoids manual calculations.

Program 5: Using strtol()

The strtol() function can convert an octal string directly into a decimal integer, which can then be printed in binary using C23’s %b specifier. This approach is concise, safe for large numbers, and avoids manual arithmetic.

#include <stdio.h>
#include <stdlib.h>

int main() {

    char octalStr[20], binary[65];
    unsigned long decimal;

    printf("Enter an octal number: ");
    scanf("%s", octalStr);

    // Convert octal string to decimal using strtol with base 8
    decimal = strtol(octalStr, NULL, 8);

    // Convert decimal to binary string using C23 %b
    sprintf(binary, "%b", decimal);

    printf("Binary equivalent: %s\n", binary);

    return 0;

}

This program reads the octal input as a string, converts it to decimal using strtol() with base 8, and then formats it as a binary string using sprintf(). It is concise, modern, and safe for large inputs.

FAQs

Q1: Can this program handle large octal numbers?
Yes, the string-based and sprintf() methods can handle very large numbers, while the arithmetic method may overflow for extremely long inputs.

Q2: Why does each octal digit map to three binary digits?
Because $2^3 = 8$, each octal digit corresponds exactly to a 3-bit binary number.

Q3: Which method is easiest for beginners?
The string-based mapping is simplest, while sprintf() is very concise but requires a C23-compliant compiler.

Conclusion

Converting octal numbers to binary is straightforward when each digit is mapped to its 3-bit binary equivalent. Arithmetic, string-based, and sprintf() methods all provide reliable results. Understanding this conversion is useful for programming, low-level operations, and working with number systems.

References & Additional Resources

Scroll to Top