📄 library_28.html
字号:
For example, when you specify the type of a function argument in a
function prototype, it makes a difference which one you use. If the
system header files declare <CODE>malloc</CODE> with an argument of type
<CODE>size_t</CODE> and you declare <CODE>malloc</CODE> with an argument of type
<CODE>unsigned int</CODE>, you will get a compilation error if <CODE>size_t</CODE>
happens to be <CODE>unsigned long int</CODE> on your system. To avoid any
possibility of error, when a function argument or value is supposed to
have type <CODE>size_t</CODE>, never declare its type in any other way.
<P>
<STRONG>Compatibility Note:</STRONG> Pre-ANSI C implementations generally used
<CODE>unsigned int</CODE> for representing object sizes and <CODE>int</CODE> for
pointer subtraction results. They did not necessarily define either
<CODE>size_t</CODE> or <CODE>ptrdiff_t</CODE>. Unix systems did define
<CODE>size_t</CODE>, in <TT>`sys/types.h'</TT>, but the definition was usually a
signed type.
<P>
<H2><A NAME="SEC484" HREF="library_toc.html#SEC484" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC484">Data Type Measurements</A></H2>
<P>
Most of the time, if you choose the proper C data type for each object
in your program, you need not be concerned with just how it is
represented or how many bits it uses. When you do need such
information, the C language itself does not provide a way to get it.
The header files <TT>`limits.h'</TT> and <TT>`float.h'</TT> contain macros
which give you this information in full detail.
<P>
<A NAME="IDX1953"></A>
<A NAME="IDX1954"></A>
<A NAME="IDX1955"></A>
<H3><A NAME="SEC485" HREF="library_toc.html#SEC485" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC485">Computing the Width of an Integer Data Type</A></H3>
<P>
The most common reason that a program needs to know how many bits are in
an integer type is for using an array of <CODE>long int</CODE> as a bit vector.
You can access the bit at index <VAR>n</VAR> with
<P>
<PRE>
vector[<VAR>n</VAR> / LONGBITS] & (1 << (<VAR>n</VAR> % LONGBITS))
</PRE>
<P>
provided you define <CODE>LONGBITS</CODE> as the number of bits in a
<CODE>long int</CODE>.
<A NAME="IDX1956"></A>
<P>
There is no operator in the C language that can give you the number of
bits in an integer data type. But you can compute it from the macro
<CODE>CHAR_BIT</CODE>, defined in the header file <TT>`limits.h'</TT>.
<P>
<DL COMPACT>
<DT><CODE>CHAR_BIT</CODE>
<DD>This is the number of bits in a <CODE>char</CODE>---eight, on most systems.
The value has type <CODE>int</CODE>.
<P>
You can compute the number of bits in any data type <VAR>type</VAR> like
this:
<P>
<PRE>
sizeof (<VAR>type</VAR>) * CHAR_BIT
</PRE>
</DL>
<P>
<A NAME="IDX1957"></A>
<A NAME="IDX1958"></A>
<A NAME="IDX1959"></A>
<H3><A NAME="SEC486" HREF="library_toc.html#SEC486" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC486">Range of an Integer Type</A></H3>
<P>
Suppose you need to store an integer value which can range from zero to
one million. Which is the smallest type you can use? There is no
general rule; it depends on the C compiler and target machine. You can
use the <SAMP>`MIN'</SAMP> and <SAMP>`MAX'</SAMP> macros in <TT>`limits.h'</TT> to determine
which type will work.
<P>
Each signed integer type has a pair of macros which give the smallest
and largest values that it can hold. Each unsigned integer type has one
such macro, for the maximum value; the minimum value is, of course,
zero.
<P>
The values of these macros are all integer constant expressions. The
<SAMP>`MAX'</SAMP> and <SAMP>`MIN'</SAMP> macros for <CODE>char</CODE> and <CODE>short
int</CODE> types have values of type <CODE>int</CODE>. The <SAMP>`MAX'</SAMP> and
<SAMP>`MIN'</SAMP> macros for the other types have values of the same type
described by the macro--thus, <CODE>ULONG_MAX</CODE> has type
<CODE>unsigned long int</CODE>.
<P>
<DL COMPACT>
<DT><CODE>SCHAR_MIN</CODE>
<DD><P>
This is the minimum value that can be represented by a <CODE>signed char</CODE>.
<P>
<DT><CODE>SCHAR_MAX</CODE>
<DD><DT><CODE>UCHAR_MAX</CODE>
<DD><P>
These are the maximum values that can be represented by a
<CODE>signed char</CODE> and <CODE>unsigned char</CODE>, respectively.
<P>
<DT><CODE>CHAR_MIN</CODE>
<DD><P>
This is the minimum value that can be represented by a <CODE>char</CODE>.
It's equal to <CODE>SCHAR_MIN</CODE> if <CODE>char</CODE> is signed, or zero
otherwise.
<P>
<DT><CODE>CHAR_MAX</CODE>
<DD><P>
This is the maximum value that can be represented by a <CODE>char</CODE>.
It's equal to <CODE>SCHAR_MAX</CODE> if <CODE>char</CODE> is signed, or
<CODE>UCHAR_MAX</CODE> otherwise.
<P>
<DT><CODE>SHRT_MIN</CODE>
<DD><P>
This is the minimum value that can be represented by a <CODE>signed
short int</CODE>. On most machines that the GNU C library runs on,
<CODE>short</CODE> integers are 16-bit quantities.
<P>
<DT><CODE>SHRT_MAX</CODE>
<DD><DT><CODE>USHRT_MAX</CODE>
<DD><P>
These are the maximum values that can be represented by a
<CODE>signed short int</CODE> and <CODE>unsigned short int</CODE>,
respectively.
<P>
<DT><CODE>INT_MIN</CODE>
<DD><P>
This is the minimum value that can be represented by a <CODE>signed
int</CODE>. On most machines that the GNU C system runs on, an <CODE>int</CODE> is
a 32-bit quantity.
<P>
<DT><CODE>INT_MAX</CODE>
<DD><DT><CODE>UINT_MAX</CODE>
<DD><P>
These are the maximum values that can be represented by, respectively,
the type <CODE>signed int</CODE> and the type <CODE>unsigned int</CODE>.
<P>
<DT><CODE>LONG_MIN</CODE>
<DD><P>
This is the minimum value that can be represented by a <CODE>signed
long int</CODE>. On most machines that the GNU C system runs on, <CODE>long</CODE>
integers are 32-bit quantities, the same size as <CODE>int</CODE>.
<P>
<DT><CODE>LONG_MAX</CODE>
<DD><DT><CODE>ULONG_MAX</CODE>
<DD><P>
These are the maximum values that can be represented by a
<CODE>signed long int</CODE> and <CODE>unsigned long int</CODE>, respectively.
<P>
<DT><CODE>LONG_LONG_MIN</CODE>
<DD><P>
This is the minimum value that can be represented by a <CODE>signed
long long int</CODE>. On most machines that the GNU C system runs on,
<CODE>long long</CODE> integers are 64-bit quantities.
<P>
<DT><CODE>LONG_LONG_MAX</CODE>
<DD><DT><CODE>ULONG_LONG_MAX</CODE>
<DD><P>
These are the maximum values that can be represented by a <CODE>signed
long long int</CODE> and <CODE>unsigned long long int</CODE>, respectively.
<P>
<DT><CODE>WCHAR_MAX</CODE>
<DD><P>
This is the maximum value that can be represented by a <CODE>wchar_t</CODE>.
@xref{Wide Character Intro}.
</DL>
<P>
The header file <TT>`limits.h'</TT> also defines some additional constants
that parameterize various operating system and file system limits. These
constants are described in section <A HREF="library_27.html#SEC454" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_27.html#SEC454">System Configuration Parameters</A>.
<P>
<A NAME="IDX1960"></A>
<A NAME="IDX1961"></A>
<A NAME="IDX1962"></A>
<A NAME="IDX1963"></A>
<H3><A NAME="SEC487" HREF="library_toc.html#SEC487" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC487">Floating Type Macros</A></H3>
<P>
The specific representation of floating point numbers varies from
machine to machine. Because floating point numbers are represented
internally as approximate quantities, algorithms for manipulating
floating point data often need to take account of the precise details of
the machine's floating point representation.
<P>
Some of the functions in the C library itself need this information; for
example, the algorithms for printing and reading floating point numbers
(see section <A HREF="library_11.html#SEC117" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC117">Input/Output on Streams</A>) and for calculating trigonometric and
irrational functions (see section <A HREF="library_17.html#SEC290" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html#SEC290">Mathematics</A>) use it to avoid round-off
error and loss of accuracy. User programs that implement numerical
analysis techniques also often need this information in order to
minimize or compute error bounds.
<P>
The header file <TT>`float.h'</TT> describes the format used by your
machine.
<P>
<H4><A NAME="SEC488" HREF="library_toc.html#SEC488" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC488">Floating Point Representation Concepts</A></H4>
<P>
This section introduces the terminology for describing floating point
representations.
<P>
You are probably already familiar with most of these concepts in terms
of scientific or exponential notation for floating point numbers. For
example, the number <CODE>123456.0</CODE> could be expressed in exponential
notation as <CODE>1.23456e+05</CODE>, a shorthand notation indicating that the
mantissa <CODE>1.23456</CODE> is multiplied by the base <CODE>10</CODE> raised to
power <CODE>5</CODE>.
<P>
More formally, the internal representation of a floating point number
can be characterized in terms of the following parameters:
<P>
<UL>
<A NAME="IDX1964"></A>
<LI>
The <DFN>sign</DFN> is either <CODE>-1</CODE> or <CODE>1</CODE>.
<P>
<A NAME="IDX1965"></A>
<A NAME="IDX1966"></A>
<LI>
The <DFN>base</DFN> or <DFN>radix</DFN> for exponentiation, an integer greater
than <CODE>1</CODE>. This is a constant for a particular representation.
<P>
<A NAME="IDX1967"></A>
<LI>
The <DFN>exponent</DFN> to which the base is raised. The upper and lower
bounds of the exponent value are constants for a particular
representation.
<A NAME="IDX1968"></A>
<P>
Sometimes, in the actual bits representing the floating point number,
the exponent is <DFN>biased</DFN> by adding a constant to it, to make it
always be represented as an unsigned quantity. This is only important
if you have some reason to pick apart the bit fields making up the
floating point number by hand, which is something for which the GNU
library provides no support. So this is ignored in the discussion that
follows.
<P>
<A NAME="IDX1969"></A>
<A NAME="IDX1970"></A>
<LI>
The <DFN>mantissa</DFN> or <DFN>significand</DFN>, an unsigned integer which is a
part of each floating point number.
<P>
<A NAME="IDX1971"></A>
<LI>
The <DFN>precision</DFN> of the mantissa. If the base of the representation
is <VAR>b</VAR>, then the precision is the number of base-<VAR>b</VAR> digits in
the mantissa. This is a constant for a particular representation.
<A NAME="IDX1972"></A>
<P>
Many floating point representations have an implicit <DFN>hidden bit</DFN> in
the mantissa. This is a bit which is present virtually in the mantissa,
but not stored in memory because its value is always 1 in a normalized
number. The precision figure (see above) includes any hidden bits.
<P>
Again, the GNU library provides no facilities for dealing with such
low-level aspects of the representation.
</UL>
<P>
The mantissa of a floating point number actually represents an implicit
fraction whose denominator is the base raised to the power of the
precision. Since the largest representable mantissa is one less than
this denominator, the value of the fraction is always strictly less than
<CODE>1</CODE>. The mathematical value of a floating point number is then the
product of this fraction, the sign, and the base raised to the exponent.
<A NAME="IDX1973"></A>
<P>
We say that the floating point number is <DFN>normalized</DFN> if the
fraction is at least <CODE>1/<VAR>b</VAR></CODE>, where <VAR>b</VAR> is the base. In
other words, the mantissa would be too large to fit if it were
multiplied by the base. Non-normalized numbers are sometimes called
<DFN>denormal</DFN>; they contain less precision than the representation
normally can hold.
<P>
If the number is not normalized, then you can subtract <CODE>1</CODE> from the
exponent while multiplying the mantissa by the base, and get another
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -