Decimal to binary conversion in C uses the repeated-division-by-2 algorithm: divide the number by 2, record the remainder (0 or 1), then divide again until the quotient reaches 0. Reading the remainders from bottom to top gives the binary equivalent. This conversion is foundational for understanding how computers store integers — every value you work with in C is binary at the hardware level.
How the Algorithm Works — Step by Step
Convert decimal 13 to binary:
- 13 ÷ 2 = 6, remainder 1
- 6 ÷ 2 = 3, remainder 0
- 3 ÷ 2 = 1, remainder 1
- 1 ÷ 2 = 0, remainder 1
- Quotient is 0 — stop. Read remainders bottom to top: 1101
Verify: 1101₂ = 1×8 + 1×4 + 0×2 + 1×1 = 8 + 4 + 0 + 1 = 13 ✓
C Program — Decimal to Binary Conversion
/* Decimal to binary in C
* Compile: gcc -ansi -Wall -Wextra dec2bin.c -o dec2bin */
#include <stdio.h>
void to_binary(int n, int bits[], int *count)
{
*count = 0;
if (n == 0) {
bits[(*count)++] = 0;
return;
}
while (n > 0) {
bits[(*count)++] = n % 2;
n /= 2;
}
}
int main(void)
{
int num, original, bits[32], count, i;
printf("Enter a decimal number: ");
if (scanf("%d", &num) != 1 || num < 0) {
printf("Please enter a non-negative integer.\n");
return 1;
}
original = num;
to_binary(num, bits, &count);
printf("Binary equivalent of %d: ", original);
for (i = count - 1; i >= 0; i--)
printf("%d", bits[i]);
printf("\n");
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra dec2bin.c -o dec2bin
./dec2bin
Sample Input and Output
Test 1:
Enter a decimal number: 13 Binary equivalent of 13: 1101
Test 2:
Enter a decimal number: 100 Binary equivalent of 100: 1100100
Test 3 — edge case (zero):
Enter a decimal number: 0 Binary equivalent of 0: 0
Test 4 — 8-bit max (255 = 11111111):
Enter a decimal number: 255 Binary equivalent of 255: 11111111
Code Explanation
- to_binary() function — separates the conversion logic from I/O. Takes the number, an array to store digits, and a pointer to the count. The pointer lets the function communicate how many bits were stored back to
main. - n % 2 — extracts the least-significant bit. This is always 0 or 1 because every integer is either even (divisible by 2) or odd.
- n /= 2 — right-shifts the number by one binary position, discarding the bit we just read.
- Reversed print — remainders are collected least-significant-bit first (13 gives 1, 0, 1, 1 in that order). Printing the array backwards gives the correct MSB-first binary string.
- Zero guard — without the
if (n == 0)check, entering 0 would skip the while loop and print nothing. The guard explicitly stores a single 0 bit.
Time and Space Complexity
| Method | Time | Space |
|---|---|---|
| Division by 2 (this program) | O(log n) | O(log n) — one array slot per bit |
| Bitwise right-shift | O(1) — fixed 32 iterations | O(1) |
What This Program Teaches
- Modulo and division for bit extraction — the same
n % 2/n /= 2pattern appears in all base-conversion programs (binary, octal, hexadecimal). - Storing digits in reverse order — a common pattern: collect values in one order, print them in reverse by walking the array backwards.
- Pointer parameter for output — passing
int *countlets a function return both a modified array and a count to the caller without using global variables. - Edge-case thinking — the zero case is the first thing a good programmer asks about. The while loop never runs for 0; the explicit guard ensures correct output.
Related Programs
- Binary to Decimal in C
- Binary to Decimal, Octal, and Hex in C
- 2’s Complement of a Binary Number in C
- Sum of Digits 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.