473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

bitness of machine using C language

How to identify if the given machine is 8/16/32 bit using C code ???

printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code??? I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ... please correct me
if I am wrong :) Actually I think to check the bit ness of machine
.... we need to check how much bit processor we are using ... and may
be that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may not
give compulsorily give the sizeof CPU register ... then how do we
check it using c code???

--Shri
Nov 14 '05 #1
5 2127
sh*********@yahoo.com (Shri) writes:
How to identify if the given machine is 8/16/32 bit using C code ???
How do you define "bitness"? For me the most useful
interpretation from a C language perspective would probably be
the number of value bits in an `unsigned int'. You can figure
this out by examining UINT_MAX in <limits.h>.
printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code??? I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ... please correct me
if I am wrong :)
Yes, both of those assumptions are true.
Actually I think to check the bit ness of machine ... we need
to check how much bit processor we are using ... and may be
that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may
not give compulsorily give the sizeof CPU register ... then how
do we check it using c code???


What are you planning to do with this information? Most of it
is of little interest from a strict C language point of view.
--
"When I have to rely on inadequacy, I prefer it to be my own."
--Richard Heathfield
Nov 14 '05 #2
Shri wrote:
How to identify if the given machine is 8/16/32 bit using C code ???

printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code??? I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ... please correct me
if I am wrong :)
To find out how much bits int uses, rather use
printf("Word length = %d Bits", sizeof(int)*CHAR_BIT);
where you need to #include <limits.h> for CHAR_BIT.
You may have 32 Bit bytes and sizeof(int) == 1.
int was AFAIK _intended_ to be the "natural" processor word.

However, with 64 Bit architectures, you often have int to be 32Bits
Even worse, with C99 you have long long which is at least 64 Bit
-- even on 32 Bit machines. So, the bit length of the data types
will not tell you much about the processor words; with high probability,
the processor words' number of bits will be divisible by CHAR_BIT.
(A similar problem existed and maybe still exists with "castrated"
32 Bit processors and long/int)
Actually I think to check the bit ness of machine
... we need to check how much bit processor we are using ... and may
be that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may not
give compulsorily give the sizeof CPU register ... then how do we
check it using c code???


As your program will have to be compiled for the respective targets,
use the information available at compile time: The compiler has to
know the target.
Read your compiler manuals. Then, if there are still problems,
ask in the compiler specific newsgroups for more informations.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3
>How to identify if the given machine is 8/16/32 bit using C code ???

The "bitness" of a particular CPU is defined by the *MARKETING
DEPARTMENT* of the manufacturer of a particular CPU. In other
words, it can (and often is) a bunch of hooey.

There are more objective measures, like the size of CPU general
registers (if it HAS general registers), the size of the data bus,
the size of the address bus, etc. However, these tend to disagree
with each other.

For example, both the 8088 and the 8086 have 16-bit registers and
almost identical instruction sets. But the 8088 has an 8-bit
external data bus and the 8086 has a 16-bit external data bus. A
compiler for the 8086 probably works fine for the 8088. A compiler
for the 8086 probably works fine for the Pentium 4 in real mode:
the programming models are virtually identical. Let's see, is the
data bus 32 or 64 bits wide (or have memory bus widths gone to 128
by now?), or does it vary by specific model? And some models have
a *36* bit-wide address bus. Now, we've got an instruction set
(and compiler that generates code for it) that runs on 8, 16, 32,
or 64-bit hardware without having to know which it's running on.
Why is the bitness of the processor relevant again?
printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code???
Yes. And it might even depend on command-line options given to
the compiler.
I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ...
please correct me
if I am wrong :)
I consider that to be two different compilers if the instruction sets
are substantially different, even if a great deal of the code is
common between them.
Actually I think to check the bit ness of machine
... we need to check how much bit processor we are using ... and may
"how much bit processor we are using" is FUZZY IN THE EXTREME.
be that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may not
give compulsorily give the sizeof CPU register ... then how do we
check it using c code???


I think you're going to have a heck of a time checking it using
*ALL* the hardware and software manuals for the processor, the
complete chip design and masks, and all the employees of the
manufacturer that had anything to do with the design of the processor.
You'll *STILL* get arguments.

Gordon L. Burditt
Nov 14 '05 #4
Gordon Burditt wrote:
How to identify if the given machine is 8/16/32 bit using C code ???

The "bitness" of a particular CPU is defined by the *MARKETING
DEPARTMENT* of the manufacturer of a particular CPU. In other
words, it can (and often is) a bunch of hooey.

There are more objective measures, like the size of CPU general
registers (if it HAS general registers), the size of the data bus,
the size of the address bus, etc. However, these tend to disagree
with each other.

For example, both the 8088 and the 8086 have 16-bit registers and
almost identical instruction sets. But the 8088 has an 8-bit
external data bus and the 8086 has a 16-bit external data bus. A
compiler for the 8086 probably works fine for the 8088. A compiler
for the 8086 probably works fine for the Pentium 4 in real mode:
the programming models are virtually identical. Let's see, is the
data bus 32 or 64 bits wide (or have memory bus widths gone to 128
by now?), or does it vary by specific model? And some models have
a *36* bit-wide address bus. Now, we've got an instruction set
(and compiler that generates code for it) that runs on 8, 16, 32,
or 64-bit hardware without having to know which it's running on.
Why is the bitness of the processor relevant again?

printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code???

Yes. And it might even depend on command-line options given to
the compiler.

I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ...
please correct me
if I am wrong :)

I consider that to be two different compilers if the instruction sets
are substantially different, even if a great deal of the code is
common between them.

Actually I think to check the bit ness of machine
... we need to check how much bit processor we are using ... and may

"how much bit processor we are using" is FUZZY IN THE EXTREME.

be that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may not
give compulsorily give the sizeof CPU register ... then how do we
check it using c code???

I think you're going to have a heck of a time checking it using
*ALL* the hardware and software manuals for the processor, the
complete chip design and masks, and all the employees of the
manufacturer that had anything to do with the design of the processor.
You'll *STILL* get arguments.


Heck, the poor OP probably just wanted to know a third of that and
will not exactly be reassured by the "(if it HAS general registers)"
part... ;-)
However: Great article!

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #5
Shri wrote:

How to identify if the given machine is 8/16/32 bit using C code ???

printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code???


On the same machine:
when I run Turbo C 2.0, sizeof(int) is 2.
when I run MSVC++ 5.0, sizeof(int) is 4.

--
pete
Nov 14 '05 #6

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

Similar topics

12
by: tmb | last post by:
1 - Is Microsoft dotnet a Virtual Machine... like the Java Virtual Machine... that will run on any operating system? 2 - If so, does Microsoft give away the dotnet development system like Sun...
9
by: dalewz | last post by:
Hi, I know that java can run on any machine. And C# is competing with java. I just came back from book store. I found that none of C# books state that its application can run everywhere. I am...
3
by: krian | last post by:
Hi, I am a IT student, present we r doing project in network programming. I got some problem here...can anybody give me a solution ..? My problem is: I have to write a program in C, it takes...
43
by: Minti | last post by:
Hi there everybody, I was just wondering that too many people choose to use language like Java because of its architecture independence, this AI is achieved because Java is as such a...
11
by: Reshat Sabiq | last post by:
Hi, I'm currently doing most of my development in Java. The only reason i'm considering alternatives is because Java is decompilable, and thus one's source code is unprotected for the most part...
1
by: rlynch99 | last post by:
On of my associates has been having problmes with a postback to self in IE using Localhost... We are trying to figure out why and how to fix it. He has already posted here, but to make sure the...
7
by: Philippe Poulard | last post by:
Hi, RefleX 0.1.3, a general-purpose XML Virtual Machine, is available here : http://reflex.gforge.inria.fr/ In this release, you'll find tutorials for mapping SQL to arbitrary complex XML...
3
by: John Kotuby | last post by:
I have just upgraded to a new development machine that came with Vista ultimate. I am developing a website with VS2005 and VB. My image and css references in my source code are all relative. For...
2
by: justinanthonyhamilton | last post by:
Hello, everyone. I recently took a class on Data Structures in C++ (using D.S. Malik's C++ Programming: Program Design Including Data Structures), and while I learned a good about specific data...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.