473,654 Members | 3,033 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Any Method to Determine Endianness at Compile Time ?

How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expressi on"),
i.e.

#if some_expression
#define TARGET_IS_LITTL E_ENDIAN
#else
#define TARGET_IS_BIG_E NDIAN

No way or some way? TIA.
Mar 19 '08 #1
19 6923
In article <27************ *************** *******@c19g200 0prf.googlegrou ps.com>,
perry.yuan <pe********@gma il.comwrote:
>How could I determine the endianness of my compile environment at
compile time, instead of run time?
You can't write a compile-time expression to determine it, because
endianness is a property of representations , not values, and there
are no objects whose representations can be examined at compile
time.

One solution is to run a program at compile time that determines
the endianness and outputs a suitable #define to a file which
you then include.

-- Richard
--
:wq
Mar 19 '08 #2
"perry.yuan " <pe********@gma il.comwrites:
How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expressi on")
One way this is commonly done is to build in two stages. First,
compile and run a test program that tests for the
implementation' s endianness. Then use the test program's output
to configure macros to be defined while building the rest of the
program.
--
"I've been on the wagon now for more than a decade. Not a single goto
in all that time. I just don't need them any more. I don't even use
break or continue now, except on social occasions of course. And I
don't get carried away." --Richard Heathfield
Mar 19 '08 #3
perry.yuan wrote:
How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expressi on"),
i.e.

#if some_expression
#define TARGET_IS_LITTL E_ENDIAN
#else
#define TARGET_IS_BIG_E NDIAN

No way or some way? TIA.
This is Question 10.16 in the comp.lang.c Frequently
Asked Questions (FAQ) list <http://www.c-faq.com/>. You've
been around this newsgroup long enough to have seen the
FAQ mentioned several dozens of times; shame on you for
not bothering to read it.

--
Er*********@sun .com
Mar 19 '08 #4
perry.yuan wrote:
How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expressi on"),
i.e.

#if some_expression
#define TARGET_IS_LITTL E_ENDIAN
#else
#define TARGET_IS_BIG_E NDIAN

No way or some way? TIA.
#include <stdio.h>
int main(void)
{
union {
char c;
int i;
} u;
u.i = 0;
u.c = 1;

if (u.i == 1)
printf("little endian\n");
else
printf("big endian\n");
}

This printsd "little endian" in the intel processor,
"big endian" in the power pc. I think it should work.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 19 '08 #5
jacob navia wrote:
perry.yuan wrote:
>How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expressi on"),
i.e.

#if some_expression
#define TARGET_IS_LITTL E_ENDIAN
#else
#define TARGET_IS_BIG_E NDIAN

No way or some way? TIA.

#include <stdio.h>
int main(void)
{
union {
char c;
int i;
} u;
u.i = 0;
u.c = 1;

if (u.i == 1)
printf("little endian\n");
else
printf("big endian\n");
}

This printsd "little endian" in the intel processor,
"big endian" in the power pc. I think it should work.
Sorry, I missed the "preprocess or" part.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 19 '08 #6
jacob wrote:
) perry.yuan wrote:
)How could I determine the endianness of my compile environment at
)compile time, instead of run time? I need a macro ("some_expressi on"),
^^^^^^^ ^^^

) int main(void)
) {
) union {
) char c;
) int i;
) } u;
) u.i = 0;
) u.c = 1;
)
) if (u.i == 1)
) printf("little endian\n");
) else
) printf("big endian\n");
) }

Isn't that a run time solution ?
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Mar 19 '08 #7
jacob navia <ja***@nospam.c omwrites:
perry.yuan wrote:
>How could I determine the endianness of my compile environment at
compile time, instead of run time? I need a macro ("some_expressi on"),

This printsd "little endian" in the intel processor,
"big endian" in the power pc. I think it should work.
I don't think you actually read the OP's question.
--
Ben Pfaff
http://benpfaff.org
Mar 19 '08 #8
In article <sl************ ********@snail. stack.nl>,
Willem <wi****@stack.n lwrote:
>) int main(void)
) {
) union {
) char c;
) int i;
) } u;
) u.i = 0;
) u.c = 1;
)
) if (u.i == 1)
) printf("little endian\n");
) else
) printf("big endian\n");
) }
>Isn't that a run time solution ?
Not if you run it at compile time :-)

-- Richard
--
:wq
Mar 19 '08 #9
Richard Tobin wrote:
In article <sl************ ********@snail. stack.nl>,
Willem <wi****@stack.n lwrote:
>) int main(void)
) {
) union {
) char c;
) int i;
) } u;
) u.i = 0;
) u.c = 1;
)
) if (u.i == 1)
) printf("little endian\n");
) else
) printf("big endian\n");
) }
>Isn't that a run time solution ?

Not if you run it at compile time :-)

-- Richard
I sent a message recognizing my error around 30 seconds
after I sent the first answer.

Again:

Excuse, it was a mistake
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 19 '08 #10

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

Similar topics

9
2753
by: Lenard Lindstrom | last post by:
I was wondering if anyone has suggested having Python determine a method's kind from its first parameter. 'self' is a de facto reserved word; 'cls' is a good indicator of a class method ( __new__ is a special case ). The closest to this I could find was the 2002-12-04 posting 'metaclasses and static methods' by Michele Simionato. The posting's example metaclass uses the method's name. I present my own example of automatic method kind...
2
8026
by: Kapil Khosla | last post by:
Dear all, I am trying to underlying implementation of virtual functions in C++. The way I understand polymorphism is class Base { public: virtual int func(); };
3
496
by: kelvSYC | last post by:
Are there any endianness concerns in C++, or does the compiler take care of those details? I ask because I'm not sure if code such as the following have consistent behavior on all platforms. typedef unsigned int u32; // sizeof(int) == 4 typedef unsigned char u8; u8 array = { 0x01, 0x23, 0x45, 0x67 }; *((u32*) array) = 0x89ABCDEF;
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
2
2129
by: Jeff User | last post by:
Here is what I have and what I would like to do: I am building a web client application. I add a web reference to some web service server to my project so that now I can call the web service. A proxy class is generated for me. This is nice. It works! Now, the method that is generated in the proxy class has an attribute preceding it that, among other things, contains the web address of the web service and the particular method at that...
18
14027
by: friend.05 | last post by:
Code to check endianness of machine
134
9020
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
6
31738
by: gg9h0st | last post by:
i really wander what makes static method special? in fact i can access a non static method statically using '::' class aclass { function anonstatic() { echo 'non static'; } static function astatic() {
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?
0
8372
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8475
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
8591
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...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
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
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.