Octal is a base-8 number system that uses digits 0–7
. Decimal, on the other hand, is the familiar base-10 system we use daily. Converting octal numbers into decimal is essential in programming when working with legacy systems, number systems, or input/output representations. In this article, we will write a C program to convert an octal number into its decimal equivalent.
Understanding The Problem
To convert octal to decimal, each digit of the octal number is multiplied by a power of 8 depending on its position, then the results are summed. For example, the octal number 31
is calculated as:3 × 8¹ + 1 × 8⁰ = 24 + 1 = 25 (decimal)
.
Our program should read an octal number from the user and compute its decimal equivalent using this logic.
Program 1: Using Loops
In this method, we process each digit of the octal number by repeatedly taking modulus and division.
#include <stdio.h>
#include <math.h>
int main() {
int n, decimal = 0, i = 0, rem;
printf("Enter an octal number: ");
scanf("%d", &n);
while (n > 0) {
rem = n % 10;
decimal += rem * pow(8, i);
n = n / 10;
i++;
}
printf("Decimal equivalent: %d\n", decimal);
return 0;
}
This program extracts each digit from the octal number, multiplies it by the corresponding power of 8, and adds it to the result.
Program 2: Using Recursion
Recursion can also simplify the conversion by repeatedly breaking down the number and calculating the sum directly.
#include <stdio.h>
#include <math.h>
int octalToDecimal(int n, int power) {
if (n == 0)
return 0;
return (n % 10) * pow(8, power) + octalToDecimal(n / 10, power + 1);
}
int main() {
int n;
printf("Enter an octal number: ");
scanf("%d", &n);
printf("Decimal equivalent: %d\n", octalToDecimal(n, 0));
return 0;
}
This program recursively computes the decimal value by extracting digits and applying powers of 8 until all digits are processed.
Program 3: Using sprintf()
Another way is to rely on C’s library functions. Here we use sscanf()
to interpret the octal number correctly, and sprintf()
to prepare its decimal form as a string.
#include <stdio.h>
int main() {
int decimal;
char octal[100], buffer[100];
printf("Enter an octal number: ");
scanf("%s", octal);
sscanf(octal, "%o", &decimal); // Convert octal string to decimal
sprintf(buffer, "%d", decimal); // Store decimal result as string
printf("Decimal equivalent: %s\n", buffer);
return 0;
}
This program is useful when the decimal result is required as a string, for instance, when saving the output to a file, displaying it as formatted text, or transferring data.
Program 4: Using strtol()
strtol()
provides a robust way to convert strings representing numbers in any base. Here, we use it to convert octal to decimal.
#include <stdio.h>
#include <stdlib.h>
int main() {
char octalStr[20];
long decimal;
printf("Enter an octal number: ");
scanf("%s", octalStr);
// Convert octal string to decimal using strtol
decimal = strtol(octalStr, NULL, 8); // Base 8 for octal
printf("Decimal equivalent: %ld\n", decimal);
return 0;
}
This method is very safe and can handle larger numbers than the standard int
type, especially if long
or long long
is used.
FAQs
Q1: What happens if the user enters digits other than 0–7?
It will give incorrect results, since valid octal numbers only include digits between 0 and 7.
Q2: Which approach is better for larger numbers?
The loop method is generally faster and simpler, while recursion provides a more elegant structure.
Q3: Why use sprintf()
instead of printing directly?sprintf()
is useful when we want the result stored as text for further processing, instead of just displaying it on the screen.
Q4: Is octal commonly used today?
Not as much as before, but it is still useful in Unix/Linux permissions and some low-level programming.
Conclusion
Converting octal numbers into decimal is a straightforward process that demonstrates how positional number systems work. Both the iterative and recursive methods provide reliable solutions, and the sprintf()
method adds flexibility when working with formatted strings. Understanding all three methods makes you confident in handling base conversions in C.
References & Additional Resources
- C Programming Tutorial – Helps in learning input, loops, and mathematical functions in C.
- GeeksforGeeks – Octal to Decimal – Explains octal to decimal conversion with examples.
- TutorialsPoint – Number Systems – A complete guide to binary, octal, decimal, and hexadecimal systems.