For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
Now Reading
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
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.
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 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.
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.
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
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.
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> |
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 |
#include <stdio.h> |
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 |
#include <stdio.h> |
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 |
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!
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.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.