size of integer

J

junky_fellow

N869, page 35 -->

"A ''plain'' int object has the natural size suggested by the
architecture of the execution environment (large enough to contain any
value in the range
INT_MIN to INT_MAX as defined in the header <limits.h>). "

How do we decide the value of INT_MIN and INT_MAX ? Does the
compiler refers to this value to determine the size of int ?

Are these values always fixed for a specific platform and who
decides these values ?
 
M

Michael Mair

N869, page 35 -->

"A ''plain'' int object has the natural size suggested by the
architecture of the execution environment (large enough to contain any
value in the range
INT_MIN to INT_MAX as defined in the header <limits.h>). "

How do we decide the value of INT_MIN and INT_MAX ?

_We_ don't. The implementation does (mostly, it takes whatever
has been given by the OS in order to work with existing libraries
but it can do as it wants).
Does the
compiler refers to this value to determine the size of int ?

Probably not. "sizeof (int) * CHAR_BIT" might be much larger
than needed due to padding bits.
Are these values always fixed for a specific platform and who
decides these values ?

See above. Usually, the platform gives preference to a certain
size in bits which usually becomes int. It may even support
padding bits for certain operations. However, the compiler's
implementor (provided that control over the standard library
is given) can freely choose within the numerical limits
given by the standard.


Cheers
Michael
 
K

Keith Thompson

N869, page 35 -->

"A ''plain'' int object has the natural size suggested by the
architecture of the execution environment (large enough to contain any
value in the range
INT_MIN to INT_MAX as defined in the header <limits.h>). "

How do we decide the value of INT_MIN and INT_MAX ? Does the
compiler refers to this value to determine the size of int ?

Are these values always fixed for a specific platform and who
decides these values ?

The <limits.h> header (which defines INT_MIN and INT_MAX) and the
compiler (which implements type int) are both part of the
implementation. They just have to be consistent with each other, and
with the requirements in the standard.

The values are decided by whoever is responsible for the
implementation. If there are multiple C implementations for a given
platform, the characteristics of the predefined types are commonly the
same across implementations, but the standard doesn't require it.
 
E

Emmanuel Delahaye

How do we decide the value of INT_MIN and INT_MAX ? Does the
compiler refers to this value to determine the size of int ?

Are these values always fixed for a specific platform and who
decides these values ?

They are platform-dependent. The implementer decides in accordance with
the features of the achitecture.

x86 real mode is 16-bit oriented
x86 protected mode is 32-bit oriented
68000 is 32-bit oriented
etc.

8051 is 8-bit oriented, that makes it a bad candidate for a
C-implementation, but with the help of external libraries (and poor
performances), implementations of C for 8051 exists (Keil etc.)


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
M

Malcolm

How do we decide the value of INT_MIN and INT_MAX ? Does the
compiler refers to this value to determine the size of int ?
The size of an int will be built into the compiler and be reflected in very
many places, such as the machine code it emits for basic operations.
INT_MIN and INT_MAX are written into limits.h, probably just before
shipping, so that the C programmer has access to the size of an integer. You
can't force a compiler to use a different size by editing these values.
Are these values always fixed for a specific platform and who
decides these values ?
int is meant to be the "natural integer size". On some machines this is
perfectly obvious. For instance many machines have 32 bit registers, and can
only implement longer or shorter integers with bit masking. Other times it
is less clear - for instance a 64 bit machine should arguably have 64-bit
ints, but many of the operations may be more efficent using halfwords of 32
bits. In these cases the compiler writer will decide what makes most sense
for his intended users - a games console compiler writer may make a
different decision to a compiler writer who is selling to engineering users.
 
J

junky_fellow

Malcolm said:
The size of an int will be built into the compiler and be reflected in very
many places, such as the machine code it emits for basic operations.
INT_MIN and INT_MAX are written into limits.h, probably just before
shipping, so that the C programmer has access to the size of an integer. You
can't force a compiler to use a different size by editing these values.
<snip>

You mean to say, that the file "limits.h" will be shipped as a part
of C compiler ?
 
D

Default User

You mean to say, that the file "limits.h" will be shipped as a part
of C compiler ?

Well, to be a C compiler it must provide the header <limits.h>. From
the C99 draft standard:


[#2] The standard headers are

<assert.h> <inttypes.h> <signal.h> <stdlib.h>
<complex.h> <iso646.h> <stdarg.h> <string.h>
<ctype.h> <limits.h> <stdbool.h> <tgmath.h>
<errno.h> <locale.h> <stddef.h> <time.h>
<fenv.h> <math.h> <stdint.h> <wchar.h>
<float.h> <setjmp.h> <stdio.h> <wctype.h>


There's no requirement that the header be a separate file, but that's
pretty typical.



Brian
 
K

Keith Thompson

<snip>

You mean to say, that the file "limits.h" will be shipped as a part
of C compiler ?

Maybe.

The standard headers (which may or may not be implemented as files),
the implementation of the standard library functions, the compiler,
the linker, and whatever else is required to support them are all part
of "the implementation". How all these parts are assembled into a
conforming C implementation, and in particular how anything is
"shipped", is not specified by the standard. If the limits.h header
is provided separately from the compiler, that's just fine as long as
everything is consistent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top