Converting a binary number to decimal, octal, and hexadecimal in C demonstrates how the same bit pattern can be expressed in different bases. Binary uses base 2, decimal uses base 10, octal uses base 8, and hexadecimal uses base 16. Once you have the decimal value, C’s printf format specifiers (%o and %X) handle octal and hex output directly — no manual conversion needed.
How Binary-to-Decimal Conversion Works — Step by Step
Convert binary 1101 to decimal:
- Read left to right: each new digit doubles the running total and adds the digit.
- Start: 0
- Digit ‘1’: 0 × 2 + 1 = 1
- Digit ‘1’: 1 × 2 + 1 = 3
- Digit ‘0’: 3 × 2 + 0 = 6
- Digit ‘1’: 6 × 2 + 1 = 13
Result: 1101₂ = 13₁₀ = 15₈ = D₁₆
C Program — Binary to Decimal, Octal, and Hexadecimal
/* Binary to Decimal, Octal, and Hexadecimal in C
* Compile: gcc -ansi -Wall -Wextra bin_conv.c -o bin_conv */
#include <stdio.h>
#include <string.h>
int binary_to_decimal(const char *bin)
{
int decimal = 0, i, len;
len = (int)strlen(bin);
for (i = 0; i < len; i++) {
decimal = decimal * 2 + (bin[i] - '0');
}
return decimal;
}
int is_valid_binary(const char *s)
{
int i;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] != '0' && s[i] != '1')
return 0;
}
return i > 0;
}
int main(void)
{
char bin[40];
int decimal;
printf("Enter a binary number: ");
scanf("%39s", bin);
if (!is_valid_binary(bin)) {
printf("Error: '%s' is not a valid binary number.\n", bin);
return 1;
}
decimal = binary_to_decimal(bin);
printf("Binary : %s\n", bin);
printf("Decimal : %d\n", decimal);
printf("Octal : %o\n", decimal);
printf("Hexadecimal : %X\n", decimal);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra bin_conv.c -o bin_conv
./bin_conv
Sample Input and Output
Test 1 — 1101 (decimal 13):
Enter a binary number: 1101 Binary : 1101 Decimal : 13 Octal : 15 Hexadecimal : D
Test 2 — 11111111 (all 8 bits set = 255):
Enter a binary number: 11111111 Binary : 11111111 Decimal : 255 Octal : 377 Hexadecimal : FF
Test 3 — 101010 (the answer to everything = 42):
Enter a binary number: 101010 Binary : 101010 Decimal : 42 Octal : 52 Hexadecimal : 2A
Test 4 — invalid input:
Enter a binary number: abc Error: 'abc' is not a valid binary number.
Code Explanation
- Horner’s method —
decimal = decimal * 2 + (bin[i] - '0')accumulates the decimal value in one pass without needing powers of 2. Each iteration doubles the running total and adds the next bit. - bin[i] – ‘0’ — converts the ASCII character
'0'or'1'to the integer 0 or 1. In ASCII,'0'is 48 and'1'is 49, so subtracting'0'gives the correct digit value. - %o and %X in printf —
%oprints an integer in octal,%Xprints in uppercase hexadecimal. No manual conversion loop is needed; the hardware handles it. - is_valid_binary() — walks the string checking each character is only
'0'or'1'. Also rejects an empty string (i > 0check). - scanf(“%39s”) — limits input to 39 characters, leaving room for the null terminator in the 40-byte buffer. Prevents buffer overflow.
Quick Conversion Reference
| Binary | Decimal | Octal | Hex |
|---|---|---|---|
| 0000 | 0 | 0 | 0 |
| 0001 | 1 | 1 | 1 |
| 1000 | 8 | 10 | 8 |
| 1010 | 10 | 12 | A |
| 1111 | 15 | 17 | F |
| 11111111 | 255 | 377 | FF |
What This Program Teaches
- Horner’s method for base conversion — multiply-and-add is the canonical way to evaluate a number in any base; it runs in O(n) with no power computation needed.
- printf format specifiers for bases —
%d,%o,%x/%Xlet you print the same integer in decimal, octal, and hex without writing conversion loops. - Character-to-digit conversion —
c - '0'is the standard idiom for converting a digit character to its integer value. - Input validation — checking preconditions (only binary digits, non-empty input) before processing prevents silent wrong answers.
Related Programs
- Binary to Decimal in C
- Decimal to Binary in C
- 2’s Complement in C
- Roman Numeral to Decimal in C
- GCD and LCM in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.
3 comments on “Binary to Decimal, Octal, and Hexadecimal in C – Conversion Program”
C program to convert a binary number to decimal,octal and hexadecimal number and back
what about fractional number…do you have any idea?…please share.