Thread: Endianness of machine

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38

    Question Endianness of machine

    hey there..

    my lecturer gave me the source code and ask me to give comment for each line..

    Code:
    #include <stdio.h> //standard I/O header file to declare printf, scanf n other things
    int main() //it tells it the return type and how many parameters there are, and what their    
                    //types are
    {
       unsigned int i = 1; //why should i use unsigned? if use 'long', still get the same answer?
       char *c = (char*)&i; //pointer? so.. c represent character point to i? (kinda blur here)
       if (*c)
           printf("Little endian");
       else
           printf("Big endian");
       getchar(); 
       return 0;
    }
    please guide me.. ^_^

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    The key is to think about your integer as being a small array of characters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Have you compiled thAt code? What is printed?
    Last edited by nimitzhunter; 01-18-2011 at 02:36 AM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by nimitzhunter View Post
    Have you compiled thAt code? What is printed?
    compiled it already..
    and the result is.. Little endian

    to be sure that the program working well,
    i substitute Little with Big and vice versa..


    Code:
    #include <stdio.h> 
    int main() 
       unsigned int i = 1; 
       char *c = (char*)&i; 
       if (*c)
           printf("Big endian");
       else
           printf("Little endian");
       getchar(); 
       return 0;
    }
    still got the same answer... Little endian

    more important here,
    this line... char *c = (char*)&i;
    i dont know how to explain it..

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Did you re-compile the code after making the changes?

  6. #6
    Registered User
    Join Date
    Jul 2009
    Location
    Malaysia, Cyberjaya
    Posts
    38
    Quote Originally Posted by Bayint Naung View Post
    Did you re-compile the code after making the changes?
    yup2.. still got the same answer.. is there anything wrong?

  7. #7
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Hmmm that's strange. Basically that code just use the pointer to point to the first byte of the integer. Why it does that is for you to find out.

    EDIT: the code you posted actually shouldn't compile. It's missing the open brace for main.
    Last edited by nimitzhunter; 01-18-2011 at 03:23 AM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    If you swapped the print statements over, and got the same answer, then you did something wrong somewhere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by naspek View Post
    compiled it already..
    and the result is.. Little endian

    to be sure that the program working well,
    i substitute Little with Big and vice versa..


    Code:
    #include <stdio.h> 
    int main() 
       unsigned int i = 1; 
       char *c = (char*)&i; 
       if (*c)
           printf("Big endian");
       else
           printf("Little endian");
       getchar(); 
       return 0;
    }
    still got the same answer... Little endian

    more important here,
    this line... char *c = (char*)&i;
    i dont know how to explain it..
    You're missing the { after main. (you need a better compiler :-)
    Look at Endianness - Wikipedia, the free encyclopedia. Come back if you still don't get it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Turing machine
    By Dante Wingates in forum General Discussions
    Replies: 5
    Last Post: 07-26-2010, 01:47 AM
  2. Will my memory leak? --school project
    By steals10304 in forum C++ Programming
    Replies: 10
    Last Post: 02-24-2010, 03:04 PM
  3. Using ip tables to forward port to virtual machine
    By sean in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-31-2009, 09:16 AM
  4. Replies: 4
    Last Post: 01-18-2008, 07:05 PM
  5. Porting from 32 bit machine to 64 bit machine!
    By anoopks in forum C Programming
    Replies: 10
    Last Post: 02-25-2005, 08:02 PM