top

Search

C Tutorial

.

UpGrad

C Tutorial

Binary to Decimal in C

Introduction

The number system plays a crucial role in computation. While several number systems exist, computation primarily depends on binary and decimal integers. 

Programming in C allows programmers to witness, experiment and learn more about binary and decimal numbers through conversions from one form to another. 

This tutorial is primarily aimed at laying down the foundations of understanding binary and decimal numbers while helping budding programmers understand how to convert binary to decimal in C with several examples from different verticals. 

Binary Numbers

Electricity is the major component of modern-day computing, and within a microchip, the electricity is either passed or omitted. This action represents ‘on’ or ‘off’, highlighting actions 1 and 0 in computing. A binary number is usually represented in base 2, emphasising that only two numbers, ‘1’ and ‘0’, exist. Even though modern-day computing allows extremely complex use of numbers and alphabets for us to work, computers only tend to understand ‘1’ and ‘0’, which are called binary numbers. 

For example, the binary representation of the number 21 will be 10101. Furthermore, the alphabet, such as ‘A’ or ‘B’, is often converted into ASCII codes. These ASCII codes are binary representations of the said alphabets, which would be ‘01000001’ for ‘A’ and ‘01000010’ for ‘B’. 

Decimal Numbers

Decimal numbers are also known as denary or base ten numbers. It represents numbers from 0 to 9, which equates to 10 digits. Usually, programmers and other computer users work with decimal numbers, following our usual approach to input figures and values. However, computers internally process these numbers in their binary form.

Algorithm to Convert Binary to Decimal in C

Converting a binary number to decimals requires loop functions and several other codes. However, before understanding the algorithm for converting binary to decimal in C, one must learn to convert the given problem manually. 

Step-by-step process of converting binary to decimal

Let’s say a programmer needs to convert the binary number 10010. Here, one must start counting the numbers from the right and assign the corresponding base 2. In other words, it should look like this. 

24

23

22

21

20

1

0

0

1

0

Once a mindmap such as this is made, the programmer must multiply the power and add the result. It must only be done for the binary digit ‘1’, while the binary digit ‘0’ is discounted. In this case, the answer will 18. 

A breakdown of the calculation is as follows-

24(2X2X2X2)+ 21(or just 2)=16+2 or 18

Examples and practice problem

  • Converting binary to decimal using loop: One of the most common ways to convert binary to decimal in C is by using a while or a for loop. These loops are primarily used to break down the binary values into single digits, which are used for further computation. 

  • Converting binary to decimal in C using array: Usually, the value of a variable is constantly updated within a loop to convert binary to decimal. However, programmers often use an array to store the values individually and calculate the rest using array calculation. 

While converting binary to decimal, one must consider the difference between general binary conversion and ASCII, which stores pre-encoded results. 

Numbers: Binary to decimal numbers are always computed in the backend. C allows programmers to understand and experiment with such conversions and write high-level C programs.

Alphabets and symbols: The American Standard Code for Information Exchange (ASCII) encodes 256 binary numbers into various symbols and alphabets in different cases.  

Convert Binary to Decimal in C Using the while Loop

Using a while loop to convert binary to decimal in C is a common practice that programmers efficiently implement in their programs. Let’s take a look at a sample example code snippet to understand how to process it.

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

int binaryToDecimal(long long binaryNumber);

int main() {
  long long binary;
  printf("Enter a binary number: ");
  scanf("%lld", &binary);
  printf("%lld in binary = %d in decimal", binary, binaryToDecimal(binary));
  return 0;
}

int binaryToDecimal(long long binaryNumber) {
  int decimal = 0;
  int position = 0;
  int remainder;

  while (binaryNumber != 0) {
    remainder = binaryNumber % 10;
    binaryNumber /= 10;
    decimal += remainder * pow(2, position);
    ++position;
  }

  return decimal;
}

As binary numbers are longer than the general integer type, it is always recommended to use the data type ‘long’ for accepting input from the user. The ‘main’ function in the code accepts the user input and sends the value to the function ‘convert(binary)’. 

The function ‘convert(binary)’ computes the result and passes the result back to the main function. The while loop continues until the value of ‘binary’ becomes zero. The value of binary is constantly changed in the loop, acting as the condition over here. The division operator divides the value of ‘binary’, and the modules function stores the remainder value of ‘binary’ when divided by 10. 

When a user inputs a binary number ‘1101’, the following calculation occurs within the while loop.

binary!=0

remainder

binary/10

position

decimal += remainder * pow(2, i)

1101 !=0

1101%10=1

1101/10=110

0

0+1 *pow(2,0)=1

110 !=0

110%10=0

110%10=11

1

1+0 *pow(2,1)=1

11 !=0

11%10=1

11%10=1

2

1+1 *pow(2,2)=5

1 !=0

1%10=1

1/10=0

3

5+1*pow(2,3)=13

0 !=0




Loop ends 

Convert Binary to Decimal in C Using for Loop

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

int convert(long long);

int main() {
  long long binaryNum;
  printf("Enter a binary number:");
  scanf("%lld", &binaryNum);
  printf("%lld in binary = %d in decimal", binaryNum, convert(binaryNum));
  return 0;
}

int convert(long long binaryNum) {
  int decimalNum = 0, i = 0, remainder;

  for (i = 0; binaryNum != 0; ++i) {
    remainder = binaryNum % 10;
    binaryNum /= 10;
    decimalNum += remainder * pow(2, i);
  }

  return decimalNum;
}

The steps to writing this program are similar to the one where the ‘while’ loop has been used. The programmer must understand the nuanced differences between a ‘while’ loop and a ‘for’ loop. Unlike the program in the ‘while’ loop, one must initialise the value, add a condition and use an increment operator in the loop, as used in the snippet,  ‘(i=0; binaryNum!=0; ++i)’. 

The underlying conversion when the user inputs a value of ‘1101’ has been broken down below. 

binaryNum!=0

remainder

binaryNum/10

i

decimalNum += remainder * pow(2, i)

1101 !=0

1101%10=1

1101/10=110

0

0+1 *pow(2,0)=1

110 !=0

110%10=0

110%10=11

1

1+0 *pow(2,1)=1

11 !=0

11%10=1

11%10=1

2

1+1 *pow(2,2)=5

1 !=0

1%10=1

1/10=0

3

5+1*pow(2,3)=13

0 !=0




Loop ends

Convert Binary to Decimal in C Using Function

#include <stdio.h>
#include <math.h>
int convert(long long);
int main() {
  long long binaryNum;
  printf("Enter a binary number: ");
  scanf("%lld", &binaryNum);
  printf("%lld in binary = %d in decimal", binaryNum, convert(binaryNum));
  return 0;
}
int convert(long long binaryNum) {
  int decimalNum = 0, i = 0, remainder;
  for (i = 0; binaryNum != 0; ++i) {
    remainder = binaryNum % 10;
    binaryNum /= 10;
    decimalNum += remainder * (int)pow(2, i);
  }
  return decimalNum;
}

For this code snippet, one must pay attention to the process, where the input is sent to the function ‘convert(long long binaryNum)’, once the user inputs a value. In the main function, the line   ‘printf("%lld in binary = %d in decimal", binaryNum, convert(binaryNum));’ sends the value to the function, which initialises it. Once the function is executed, the line ‘return decimalNum’ returns the converted value to the main function, which is then printed as a result. 

The table highlights a breakdown of the calculation for the given code snippet when the user inputs the value ‘1101’.

binaryNum!=0

remainder

binaryNum/10

i

decimalNum += remainder * (int)pow(2, i)

1101 !=0

1101%10=1

1101/10=110

0

0+1 *pow(2,0)=1

110 !=0

110%10=0

110%10=11

1

1+0 *pow(2,1)=1

11 !=0

11%10=1

11%10=1

2

1+1 *pow(2,2)=5

1 !=0

1%10=1

1/10=0

3

5+1*pow(2,3)=13

0 !=0




Loop ends

Conclusion 

A veteran programmer is a person who can act as a translator between a human and a machine. With a strong grasp of the machine-readable binary number system along with compiled code snippets, you’re now several steps closer to being one such experienced programmer.  

On the other hand, if you’re aiming to further strengthen your grasp on programming skills, check out upGrad’s Full Stack Software Development Bootcamp, which aims to provide aspirants with a comprehensive knowledge of programming to become an efficient full stack developer. 

Being one of the most coveted professions, full stack development is on the path to becoming India’s leading profession. Hence, this program will prepare you to acquire exceptional skills and even better work opportunities in the long run!

FAQs

1. While converting binary to decimal in C, should we consider 2 to the power 0 as one or zero?

A. When converting binary to decimal in C using loop, two power zero must be computed as 1. It is a common occurrence while converting binary to decimal in C or any other language, and one must always pay attention to this detail. 

2. Why is it necessary to include the header file ‘math.h’ to convert binary to decimal in C?

A. Loops use the power function to compute the result. In C, the power function is inherently absent. Hence programmers must include the ‘math.h’ header file, where the mathematical calculation of the power function is pre-determined along with many other math functions.

3. Can we only use a function to convert binary to decimal in C?

A. Loops are mandatory to break down the user input. Therefore, only the use of the function might not suffice. Instead, programmers can club functions and loops together.  

Leave a Reply

Your email address will not be published. Required fields are marked *