Numbering Systems in C
The C programming language uses several different numbering systems for representing numbers, including decimal (base 10), binary (base 2), octal (base 8), and hexadecimal (base 16).
Decimal numbers are the most commonly used numbering system in C and are represented as we use in daily life . For example, the decimal number 42 represents the value forty-two in the decimal numbering system.
Binary numbers are represented using only the digits 0 and 1. These digits represent the presence or absence of a power of 2. For example, the binary number 1010 represents the decimal value 10.
Octal numbers are represented using the digits 0 through 7. They are often used to represent the permissions on a file in a Unix-like operating system. For example, the octal number 755 represents the permissions rwxr-xr-x.
Hexadecimal numbers are represented using the digits 0 through 9 and the letters A through F. They are often used to represent large binary numbers more compactly. For example, the hexadecimal number 1F represents the binary number 00011111.
We can also represent these number system in C by prefixing them with specific notations
decimal-> no prefix
binary-> prefix 0b
octal-> prefix 0
hexadecimal-> prefix 0x
For example
unsigned int decimal_num = 42;
unsigned int binary_num = 0b101010;
unsigned int octal_num = 052;
unsigned int hexadecimal_num = 0xFF;
C also provides the ability to convert between number systems using several functions such as,
strtol() : Converts a string representation of a number to an integer
strtoul() : Converts a string representation of an unsigned integer
We can also use various formatting specifiers like %d, %i, %o, %u, %x, %X, in the printf and scanf functions to print and read numbers in different number systems.
Leave a Comment