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

24 May 2023 Balmiki Mandal 0 C Programming

How to Prove Endianness Using a Character Pointer

Endianness is the order in which bytes are stored in memory. There are two types of endianness: big endian and little endian.

  • In big endian, the most significant byte is stored first.
  • In little endian, the least significant byte is stored first.

To prove that we are working on little endian or big endian using character pointer, we can use the following code:

#include<stdio.h>

int main() {
  // Declare a variable of type int.
  int n = 10;

  // Convert the int to a character pointer.
  char *p = (char *)&n;

  // Check the value of the first byte of the pointer.
  if (*p == 0x0a) {
    // We are working on a little endian machine.
  } else if (*p == 0x10) {
    // We are working on a big endian machine.
  }

  return 0;
}

This code will print the following output on a little endian machine:

We are working on a little endian machine.

And the following output on a big endian machine:

We are working on a big endian machine.

I hope this helps! Let me know if you have any other questions.

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.