Write a program to prove we are working little endian or big endian using character pointer in c

03 Sep 2022 Balmiki Mandal 0 C Programming

Detect Endianness in C using Character Pointers

Endianness refers to the order in which the bytes of a multi-byte data type (such as an integer or a floating-point number) are stored in memory.

There are two common types of endianness:

Big-endian and little-endian.

In a big-endian system, the most significant byte (i.e., the byte containing the highest-order bits) is stored at the lowest address in memory, while the least significant byte (i.e., the byte containing the lowest-order bits) is stored at the highest address. This is sometimes referred to as "network byte order" because it is the order used in network protocols.

In a little-endian system, the least significant byte is stored at the lowest address, while the most significant byte is stored at the highest address.

The choice of endianness is arbitrary, but it can have important implications for data interchange between different systems. For example, if a big-endian system sends data to a little-endian system, the bytes will be in the wrong order and the receiving system may interpret the data incorrectly. Therefore, it is important to understand the endianness of the systems you are working with and to convert the data to the appropriate byte order when necessary.

To prove whether we are working on a little endian or big endian machine using a character pointer, we can use the following C program:

C programming
#include<stdio.h>

int main() {
  int n = 10; // A 32-bit integer
  char *p = (char *)&n; // A character pointer to the integer

  // Check the value of the first byte of the character pointer
  if (*p == 0x0a) {
    printf("We are working on a little endian machine.\n");
  } else if (*p == 0x10) {
    printf("We are working on a big endian machine.\n");
  } else {
    printf("Unknown endianness.\n");
  }

  return 0;
}

This program will work by first casting the integer n to a character pointer. This will make the character pointer point to the first byte of the integer. The program then checks the value of the first byte of the character pointer. If the value is 0x0a, then the machine is little endian. If the value is 0x10, then the machine is big endian. If the value is something else, then the program will print an error message.

To compile and run this program, you can use the following commands:

gcc -o endianness endianness.c
./endianness

The output of the program will be one of the following lines:

We are working on a little endian machine.
We are working on a big endian machine.
Unknown endianness.

The output of the program will tell you whether your machine is little endian or big endian.

Top Resources


Little Endian and Big Endian in c

write a program to prove that we are working on  big Endian using union

Write a Program to prove we are working on little Endian environment using a union

Write a program to prove we are working little endian or big endian using character pointer in c

Prove That We are working on Little Endian and Big Endian Using Character Pointer

Enroll Now:

C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"

Contact Us:

  • For any inquiries, please email us at [[email protected]].
  • Follow us on insta  [ electro4u_offical_ ] for updates and tips.

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.