C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

Binary, Octal And Hexadecimal

In this tutorial, we'll discuss how to work with binary, octal, and hexadecimal numbers in the C programming language. These number systems are essential for understanding computer systems and low-level programming tasks.

  • Representation of numbers in different bases:
  • Binary (base 2): Uses 0 and 1
  • Octal (base 8): Uses 0-7
  • Decimal (base 10): Uses 0-9 (default in C)
  • Hexadecimal (base 16): Uses 0-9 and A-F (or a-f)
  • Declaring and initializing integer literals in different bases:

To declare and initialize integer literals in binary, octal, or hexadecimal, use the following prefixes:

  • Binary: 0b or 0B
  • Octal: 0
  • Hexadecimal: 0x or 0X
int binaryNumber = 0b1010;       // 10 in decimal
int octalNumber = 012;           // 10 in decimal
int hexadecimalNumber = 0xA;     // 10 in decimal
  • Printing integers in different bases:

To print integers in different bases, use format specifiers with the printf() function:

  • Binary: There is no built-in format specifier for binary, so you will need to implement a custom function.
  • Octal: %o
  • Decimal: %d
  • Hexadecimal: %x (lowercase) or %X (uppercase)
#include <stdio.h>

void printBinary(int num) {
    if (num > 1) {
        printBinary(num / 2);
    }
    printf("%d", num % 2);
}

int main() {
    int number = 10;

    printf("Binary: ");
    printBinary(number);
    printf("\n");

    printf("Octal: %o\n", number);
    printf("Decimal: %d\n", number);
    printf("Hexadecimal: %x\n", number); // Use %X for uppercase

    return 0;
}

The output will be:

Binary: 1010
Octal: 12
Decimal: 10
Hexadecimal: a

That's it for our tutorial on binary, octal, and hexadecimal numbers in the C programming language. Understanding different number systems and how to work with them in C is essential for low-level programming tasks, debugging, and working with memory addresses or bitwise operations.

  1. C program for converting decimal to binary:

    #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;
    }
    
    • Converts a decimal number to its binary representation.
  2. Converting binary to decimal in C program:

    #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;
    }
    
    • Converts a binary number to its decimal representation.
  3. C code for handling octal numbers:

    #include <stdio.h>
    
    int main() {
        int octalNumber = 075;
        printf("Octal representation: %o\n", octalNumber);
    
        return 0;
    }
    
    • Shows the octal representation of a number.
  4. Manipulating hexadecimal numbers in C:

    #include <stdio.h>
    
    int main() {
        int hexadecimalNumber = 0xA;
        printf("Hexadecimal representation: %X\n", hexadecimalNumber);
    
        return 0;
    }
    
    • Demonstrates manipulating and printing a hexadecimal number.