Q:

Write a C program to check the endianness of the system

0

Write a C program to check the endianness of the system

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
int main(void)
{
    uint32_t u32RawData;
    uint8_t *pu8CheckData;
    u32RawData = 0x11223344; //Assign data
    pu8CheckData = (uint8_t *)&u32RawData; //Type cast
    if (*pu8CheckData == 0x44) //check the value of lower address
    {
        printf("little-Endian");
    }
    else if (*pu8CheckData == 0x11) //check the value of lower address
    {
        printf("big-Endian");
    }
    return 0;
}

Method 2:

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
typedef union
{
    uint32_t u32RawData;  // integer variable
    uint8_t  au8DataBuff[4]; //array of character
} RawData;
int main(void)
{
    RawData uCheckEndianess;
    uCheckEndianess.u32RawData = 0x11223344; //assign the value
    if (uCheckEndianess.au8DataBuff[0] == 0x44) //check the array first index value
    {
        printf("little-endian");
    }
    else if (uCheckEndianess.au8DataBuff[0] == 0x11) //check the array first index value
    {
        printf("big-endian");
    }
    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Embedded C interview questions and answers (2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Convert little-endian to big-endian vice ve... >>
<< What is big-endian and little-endian?...