Skip to content

Instantly share code, notes, and snippets.

@k4rtik
Created December 9, 2011 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k4rtik/1453785 to your computer and use it in GitHub Desktop.
Save k4rtik/1453785 to your computer and use it in GitHub Desktop.
Program to check the endianness of a machine. - January 9, 2011
/*
* endian.c
* Program to check the endianness of a machine.
*
* Copyright (C) 2011 - Kartik Singhal
*
* License: GNU General Public License version 3
*
*/
#include <stdio.h>
int main()
{
int num = 0x1451;
char* p = (char*) &num;
char* q = p+1;
printf("First byte: %x\n", *p);
printf("Second byte: %x\n", *q);
(*p == 0x51) ? printf("This machine is Little Endian\n") :\
printf("This machine is Big Endian\n") ;
return 0;
}
/*
* This program uses some reference from:
* http://sites.google.com/site/insideoscore/endianness
* and the author wants to acknowledges the same.
*/
/*
* OUTPUT (on a an Intel Core 2 Duo based machine):
*******************************************************************************
kartik@PlatiniumLight:~$ gcc endian.c
kartik@PlatiniumLight:~$ ./a.out
First byte: 51
Second byte: 14
This machine is Little Endian
********************************************************************************
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment