Like Tree1Likes
  • 1 Post By anduril462

host byte order program not running

This is a discussion on host byte order program not running within the Linux Programming forums, part of the Platform Specific Boards category; hello everyone, i m writing a c program to find the host byte error but it is not executing....cud any1 ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Smile host byte order program not running

    hello everyone, i m writing a c program to find the host byte error but it is not executing....cud any1 plz plz plz help me out in sorting this out.
    thanks a lot in advance




    Code:
    #include<stdio.h>
    
    int main()
    {
    int a,i;
    char b[sizeof(a)];
    
    printf("enter b");
    for(i=0;i<sizeof(a);i++)
    {
    scanf("%c",&b[i]);
    }
    
    if(sizeof(a)==4)
    {
    	if(b[1]==1 && b[2]==2 && b[3]==3 && b[4]==4)
    	{
    		printf("big endian");
    	}
    	else if(b[1]==4 && b[2]==3 && b[3]==2 && b[1]==1)
    	{
    		printf("little endian");
    	}
    }
    else
    	{
    		printf("invalid");
    	}
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,193
    If, by "not executing" you mean the program is not producing output, then presumably the characters you are inputting are not meeting the conditions to produce output.

    On the if block that prints "big endian" or "little endian", try adding an else clause that prints some indication if neither of the conditions your code is testing are true.

    Also, if your input is 1234 then (assuming an ASCII character set) the elements of array b will be 49, 50, 51, and 52.

    There is the incidental problem that your code is not testing host byte order. The only thing it tests is the order you enter (presumably) characters 1234 from the keyboard (stdin). That has no relationship to host byte order.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    i tried adding an else clause but still it is not producing any output....

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    Quote Originally Posted by cute_kuddi View Post
    cud any1 plz plz plz
    SMS speak is not so cute, cute_kuddi. Please talk like a normal person.

    You have several problems in your code (since n00bs often miss this, the red words are links, click them):
    1. You said main would return an int, but you don't. Add a return 0; as the last line of main. Read up on why it matters here and here.
    2. You are reading in individual characters into the slots in b. That means entering 1234 gives you '1', '2', '3', '4' (the ASCII values), not the digits 1, 2, 3, 4.
    3. You don't even need to take user input for this to work. Actually, certain user input can cause this function to not work.
    4. You don't need to check sizeof(a), it will only make your code not work on non-32 bit machines.

    Your code betrays a serious lack of understanding of how computers and the C language represent data. You need to do some serious reading on binary numbers and endianness, and some C tutorials on user IO and character/string data vs. integer data.

    Out of curiosity, did you try Google: how to check endianness - Google Search
    Salem likes this.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,438
    I just saw your other post. Read the man pages for htonl, htons, ntohl and ntohs. You might find them useful for your purposes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping byte order, aka Big endian?
    By Alexlf in forum C Programming
    Replies: 7
    Last Post: 01-15-2011, 09:44 AM
  2. convert struct to host byte order
    By jean.yves in forum C Programming
    Replies: 13
    Last Post: 12-15-2010, 02:06 PM
  3. byte order change
    By onebrother in forum C Programming
    Replies: 1
    Last Post: 08-06-2007, 05:40 AM
  4. changing byte order of an unsigned long?
    By stustu92 in forum C Programming
    Replies: 6
    Last Post: 01-27-2006, 05:21 AM
  5. byte and bit order question
    By chunlee in forum C Programming
    Replies: 7
    Last Post: 11-07-2004, 12:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21