Advertisement
ringneckparrot

EndiannessTest documented

Apr 9th, 2012
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. /****************************************************************************
  2.     ringneckparrot (c)
  3.     License: http://creativecommons.org/licenses/by-nc-sa/3.0/
  4.    
  5.     Contact Me:
  6.     Email: ringneckparrot@hotmail.com
  7.     Facebook: http://www.facebook.com/ringneckparrot
  8.     Twitter ID: pp4rr0t
  9.     SecurityTube: http://www.securitytube.net/user/ringneckparrot
  10.  
  11. ****************************************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #define LITTLE 0 // LITTLE = Little Endian
  17. #define BIG 1 // BIG = Big Endian
  18.  
  19. int EndiannessTest() //Create a function EndiannessTest() which tests the Endianness and returns an integer (0 for Little Endian | 1 for Big Endian)
  20. {
  21.     // Declare an integer i and assign a value of 1 to it
  22.     int i = 1;
  23.     // Create a pointer p and make it point to the address of i, we force the compiler to treat it as a character
  24.     char *p = (char *) &i;
  25.  
  26.     //Check if the value where p points is 1   
  27.     if(*p == 1)
  28.         //if yes, it returns the LITTLE (Little Endian) = 0
  29.         return LITTLE;
  30.     else
  31.         //else, it returns the BIG (Big Endian) = 1
  32.         return BIG;
  33.    
  34. }
  35.  
  36. int main() //Our main() function
  37. {
  38.     // Create an integer called Endian
  39.     int Endian;
  40.  
  41.     // Assign Endian with the result of the function EndiannessTest()
  42.     Endian = EndiannessTest();
  43.  
  44.    
  45.     //If the result of Endian is LITTLE (= 0), we have a Little Endian system
  46.     if (Endian == LITTLE)
  47.         //We print to the screen "Little Endian", followed by a new line symbol "\n"
  48.         printf("Little Endian\n");
  49.     // If the result of Endian is BIG (= 1), we have a Big Endian system
  50.     if (Endian == BIG)
  51.         //We print to the screen "Big Endian", followed by a new line symbol "\n"
  52.         printf("Big Endian\n");
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement