473,657 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Endianness of machine

Code to check endianness of machine

Feb 16 '06 #1
18 14027
friend.05 schrieb:
Code to check endianness of machine


You forgot to include your code.
Please make sure that it is a compiling piece of
code and explain your exact problem with it.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 16 '06 #2
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1 234",4))
printf("Big Endian");
else
printf("little Endian");
}

Feb 16 '06 #3


friend.05 wrote:
Code to check endianness of machine

Its in C-FAQ

Check Q 20.9 at
http://www.eskimo.com/~scs/C-faq/top.html

- Ravi

Feb 16 '06 #4
"Sreekanth" <sr************ ********@gmail. com> writes:
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1 234",4))
printf("Big Endian");
else
printf("little Endian");
}


Please provide context, even if the previous post was only one line.
You *have* read <http://cfaj.freeshell. org/google/>, haven't you?

The program makes the following unwarranted and unnecessary
assumptions:

ASCII character set

sizeof(long)==4

Big-endian and little-endian are the only possibilities

Output will appear even without a new-line

It's acceptable to fall off the end of main() without returning a
value (true in C99, but still poor style)

And a minor point: "int main()" is acceptable, but the more explicit
"int main(void)" is preferred.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 16 '06 #5

Keith Thompson wrote:
"Sreekanth" <sr************ ********@gmail. com> writes:
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1 234",4))
printf("Big Endian");
else
printf("little Endian");
}


Please provide context, even if the previous post was only one line.
You *have* read <http://cfaj.freeshell. org/google/>, haven't you?

The program makes the following unwarranted and unnecessary
assumptions:

ASCII character set

sizeof(long)==4

Big-endian and little-endian are the only possibilities

Output will appear even without a new-line

It's acceptable to fall off the end of main() without returning a
value (true in C99, but still poor style)

And a minor point: "int main()" is acceptable, but the more explicit
"int main(void)" is preferred.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Sorry for the bad post. I am learning C programming been a java
programmer all my life. This is the first time I am using USENET group.
So that is not an excuse for a poor post. Anyway I will see to that it
doesnt repeat

Feb 16 '06 #6
Ico
friend.05 <hi**********@g mail.com> wrote:
Code to check endianness of machine


Usenet etiquette
--
:wq
^X^Cy^K^X^C^C^C ^C
Feb 16 '06 #7
On Wed, 15 Feb 2006 21:42:15 -0800, Sreekanth wrote:
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1 234",4))
printf("Big Endian");
else
printf("little Endian");
}


Rather too many assumptions here (as already pointed out). Endian-ness is
essentially the relationship between byte addressing and artimetic
significance so you can investigate it like this:

#include <stdio.h>

/* Edit this if you have an old compiler: */
typedef unsigned long long int integer;

union {
integer number;
char bytes[sizeof(integer)];
} u;

int main(avoid)
{
int b;
u.number = (integer)0;
/* assign 1 to LSB, 2 to the next and so on... *.
for (b = 0; b < sizeof(integer) ; b++)
u.number |= (integer)(b + 1) << (8 * b);
/* convert byte numbers to digits and print in address order */
for (b = 0; b < sizeof(integer) ; b++)
printf("%c", '0' + u.bytes[b]);
printf("\n");
return 0;
}

Although there n! possible results on an n-byte architecture, sanity and
engineering mean that you are unlikely to come across more than 3.

--
Ben.

Feb 16 '06 #8
"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"Sreekanth" <sr************ ********@gmail. com> writes:
#include <stdio.h>

int main()
{
long x = 0x34333231;
char *y = (char *) &x;

if(strncmp(y,"1 234",4))
printf("Big Endian");
else
printf("little Endian");
}
The program makes the following unwarranted and unnecessary
assumptions:

ASCII character set

sizeof(long)==4
I think sizeof(long)>=4 .
Big-endian and little-endian are the only possibilities

Output will appear even without a new-line

It's acceptable to fall off the end of main() without returning a
value (true in C99, but still poor style)

And a minor point: "int main()" is acceptable, but the more explicit
"int main(void)" is preferred.


Also, the OP forgot to include <string.h>
Feb 16 '06 #9
"Ben Bacarisse" <be********@bsb .me.uk> wrote in message
news:pa******** *************** *****@bsb.me.uk ...
Endian-ness is
essentially the relationship between byte addressing and artimetic
significance so you can investigate it like this:
Yes, allow me some comments.
#include <stdio.h>

/* Edit this if you have an old compiler: */
typedef unsigned long long int integer;

union {
integer number;
char bytes[sizeof(integer)];
} u;

int main(avoid)
Is this void or am i missing something?
{
int b;
u.number = (integer)0;
/* assign 1 to LSB, 2 to the next and so on... *.
for (b = 0; b < sizeof(integer) ; b++)
u.number |= (integer)(b + 1) << (8 * b);
/* convert byte numbers to digits and print in address order */
for (b = 0; b < sizeof(integer) ; b++)
printf("%c", '0' + u.bytes[b]);
That will not print digits if sizeof(integer) exceeds 9.
printf("\n");
return 0;
}

Although there n! possible results on an n-byte architecture, sanity and
engineering mean that you are unlikely to come across more than 3.

Feb 16 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
11625
by: Case | last post by:
#include <string.h> int i; /* 4-byte == 4-char */ char data = { 0x78, 0x56, 0x34, 0x12 }; int main() { memcpy(&i, data, 4); /*
15
2021
by: T Koster | last post by:
Hi group, I'm having some difficulty figuring out the most portable way to read 24 bits from a file. This is related to a Base-64 encoding. The file is opened in binary mode, and I'm using fread to read three bytes from it. The question is though, where should fread put this? I have considered two alternatives, but neither seem like a good idea: In most cases, the width of a char is 8 bits, so an array of 3 chars
7
4221
by: bush | last post by:
hi folks I am new to this group.i need to know how i can find the endianness of my system.help me out.
2
4025
by: SSM | last post by:
Hi, Does C standard comment about "Endianness" to be used to store a structure/union variables? Thanks & Regards, Mehta
72
11577
by: gamehack | last post by:
Hi all, I was thinking today, suppose we have the number n = 0xAB 0xFF which is equivalent to 44031 in decimal. In big endian it will be stored as 10101011 11111111 but in little endian it will be 11111111 10101011 If we then apply a bit shift n << 2; that would give us completely
7
3767
by: bob_jenkins | last post by:
When comparing strings, if a machine is big-endian, even if strings are not aligned, you can do some shifts then do word comparisons rather than byte-by-byte comparisons. Little-endian machines, though, haven't much choice but to read strings byte by byte during comparisons. The expense of those shifts is a little less than the cost of reading things byte-by-byte on 32-bit machines. But on 64-bit machines, the shift-and-compare-words...
18
2813
by: Indian.croesus | last post by:
Hi, If I am right Endianness is CPU related. I do not know if the question is right in itself but if it is then how does C handle issues arising out of Endianness. I understand that if we pass structures using sockets across platforms, we need to take care of Endianness issues at the application level. But for example, for the code using bitwise AND to figure out if a number is odd or even, how does C know the LSB position?
5
6799
by: Rahul | last post by:
Hi Everyone, I have a program unit which does >and << of an integer which is of 4 bytes length. The logic of shifting and action based on the result, assumes that the system is big-endian. Accordingly, if i need the program to work fine in a little-endian system. I understand that the code needs to be changed. ( I couldn't find any statement in C90 about endianness, hence i'm assuming that c programs are not portable if the endianness...
19
6924
by: perry.yuan | last post by:
How could I determine the endianness of my compile environment at compile time, instead of run time? I need a macro ("some_expression"), i.e. #if some_expression #define TARGET_IS_LITTLE_ENDIAN #else #define TARGET_IS_BIG_ENDIAN No way or some way? TIA.
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.