C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In the C programming language, binary, octal, and hexadecimal are different number systems that can be used to represent numerical values. Here is a brief overview of each of these number systems:
Binary: Binary is a base-2 number system, which means it uses only two digits: 0 and 1. In C, you can represent binary numbers by prefixing them with 0b
or 0B
. For example, int binaryNum = 0b1010;
declares an integer variable binaryNum
with a value of 10 in binary (1010 in decimal).
Octal: Octal is a base-8 number system, which means it uses the digits 0 to 7. In C, you can represent octal numbers by prefixing them with 0
(zero). For example, int octalNum = 012;
declares an integer variable octalNum
with a value of 10 in octal (12 in decimal).
Hexadecimal: Hexadecimal is a base-16 number system, which means it uses the digits 0 to 9 and the letters A to F (which represent values 10 to 15). In C, you can represent hexadecimal numbers by prefixing them with 0x
or 0X
. For example, int hexNum = 0x1F;
declares an integer variable hexNum
with a value of 31 in hexadecimal (1F in hexadecimal).
To convert between these number systems, you can use various conversion functions in C. For example, you can convert a binary or hexadecimal string to an integer using the strtol
function:
char binaryStr[] = "1010"; int binaryNum = strtol(binaryStr, NULL, 2); // convert binary string to integer printf("Binary number %s is %d in decimal\n", binaryStr, binaryNum); char hexStr[] = "1F"; int hexNum = strtol(hexStr, NULL, 16); // convert hexadecimal string to integer printf("Hexadecimal number %s is %d in decimal\n", hexStr, hexNum);
You can also convert an integer to a binary, octal, or hexadecimal string using the sprintf
function:
int decimalNum = 31; char binaryStr[33]; sprintf(binaryStr, "%b", decimalNum); // convert decimal number to binary string printf("Decimal number %d is %s in binary\n", decimalNum, binaryStr); char octalStr[12]; sprintf(octalStr, "%o", decimalNum); // convert decimal number to octal string printf("Decimal number %d is %s in octal\n", decimalNum, octalStr); char hexStr[9]; sprintf(hexStr, "%X", decimalNum); // convert decimal number to hexadecimal string printf("Decimal number %d is %s in hexadecimal\n", decimalNum, hexStr);
Understanding how to work with binary, octal, and hexadecimal numbers is important for many aspects of computer programming, including bitwise operations and working with hardware devices.
Converting decimal to binary in C:
#include <stdio.h> void decimalToBinary(int n) { if (n > 1) decimalToBinary(n / 2); printf("%d", n % 2); } int main() { int decimalNumber = 10; printf("Binary representation: "); decimalToBinary(decimalNumber); return 0; }
C program for octal number manipulation:
#include <stdio.h> int main() { int octalNumber = 075; printf("Octal representation: %o\n", octalNumber); return 0; }
Hexadecimal constants in C programming:
#include <stdio.h> int main() { int hexadecimalNumber = 0xA; printf("Hexadecimal representation: %X\n", hexadecimalNumber); return 0; }
C code for binary to decimal conversion:
#include <stdio.h> #include <math.h> int binaryToDecimal(long long binaryNumber) { int decimalNumber = 0, i = 0, remainder; while (binaryNumber != 0) { remainder = binaryNumber % 10; binaryNumber /= 10; decimalNumber += remainder * pow(2, i); ++i; } return decimalNumber; } int main() { long long binaryNumber = 1010; printf("Decimal representation: %d\n", binaryToDecimal(binaryNumber)); return 0; }
Using hexadecimal literals in C:
#include <stdio.h> int main() { int hexadecimalLiteral = 0x1A; printf("Hexadecimal literal: %X\n", hexadecimalLiteral); return 0; }