📄 library_18.html
字号:
which is <CODE>6.5</CODE> minus <CODE>6.9</CODE>.<P>The absolute value of the result is less than or equal to half theabsolute value of the <VAR>denominator</VAR>. The difference between<CODE>fmod (<VAR>numerator</VAR>, <VAR>denominator</VAR>)</CODE> and <CODE>drem(<VAR>numerator</VAR>, <VAR>denominator</VAR>)</CODE> is always either<VAR>denominator</VAR>, minus <VAR>denominator</VAR>, or zero.<P>If <VAR>denominator</VAR> is zero, <CODE>drem</CODE> fails and sets <CODE>errno</CODE> to<CODE>EDOM</CODE>.<P><A NAME="IDX1327"></A><H2><A NAME="SEC305" HREF="library_toc.html#SEC305" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC305">Integer Division</A></H2><P>This section describes functions for performing integer division. Thesefunctions are redundant in the GNU C library, since in GNU C the <SAMP>`/'</SAMP>operator always rounds towards zero. But in other C implementations,<SAMP>`/'</SAMP> may round differently with negative arguments. <CODE>div</CODE> and<CODE>ldiv</CODE> are useful because they specify how to round the quotient:towards zero. The remainder has the same sign as the numerator.<P>These functions are specified to return a result <VAR>r</VAR> such that<CODE><VAR>r</VAR>.quot*<VAR>denominator</VAR> + <VAR>r</VAR>.rem</CODE> equals<VAR>numerator</VAR>.<A NAME="IDX1328"></A><P>To use these facilities, you should include the header file<TT>`stdlib.h'</TT> in your program.<P><A NAME="IDX1329"></A><U>Data Type:</U> <B>div_t</B><P>This is a structure type used to hold the result returned by the <CODE>div</CODE>function. It has the following members:<P><DL COMPACT><DT><CODE>int quot</CODE><DD>The quotient from the division.<P><DT><CODE>int rem</CODE><DD>The remainder from the division.</DL><P><A NAME="IDX1330"></A><U>Function:</U> div_t <B>div</B> <I>(int <VAR>numerator</VAR>, int <VAR>denominator</VAR>)</I><P>This function <CODE>div</CODE> computes the quotient and remainder fromthe division of <VAR>numerator</VAR> by <VAR>denominator</VAR>, returning theresult in a structure of type <CODE>div_t</CODE>.<P>If the result cannot be represented (as in a division by zero), thebehavior is undefined.<P>Here is an example, albeit not a very useful one.<P><PRE>div_t result;result = div (20, -6);</PRE><P>Now <CODE>result.quot</CODE> is <CODE>-3</CODE> and <CODE>result.rem</CODE> is <CODE>2</CODE>.<P><A NAME="IDX1331"></A><U>Data Type:</U> <B>ldiv_t</B><P>This is a structure type used to hold the result returned by the <CODE>ldiv</CODE>function. It has the following members:<P><DL COMPACT><DT><CODE>long int quot</CODE><DD>The quotient from the division.<P><DT><CODE>long int rem</CODE><DD>The remainder from the division.</DL><P>(This is identical to <CODE>div_t</CODE> except that the components are oftype <CODE>long int</CODE> rather than <CODE>int</CODE>.)<P><A NAME="IDX1332"></A><U>Function:</U> ldiv_t <B>ldiv</B> <I>(long int <VAR>numerator</VAR>, long int <VAR>denominator</VAR>)</I><P>The <CODE>ldiv</CODE> function is similar to <CODE>div</CODE>, except that thearguments are of type <CODE>long int</CODE> and the result is returned as astructure of type <CODE>ldiv</CODE>.<P><A NAME="IDX1333"></A><A NAME="IDX1334"></A><A NAME="IDX1335"></A><A NAME="IDX1336"></A><H2><A NAME="SEC306" HREF="library_toc.html#SEC306" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC306">Parsing of Numbers</A></H2><P>This section describes functions for "reading" integer andfloating-point numbers from a string. It may be more convenient in somecases to use <CODE>sscanf</CODE> or one of the related functions; seesection <A HREF="library_11.html#SEC145" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_11.html#SEC145">Formatted Input</A>. But often you can make a program more robust byfinding the tokens in the string by hand, then converting the numbersone by one.<P><H3><A NAME="SEC307" HREF="library_toc.html#SEC307" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC307">Parsing of Integers</A></H3><A NAME="IDX1337"></A><P>These functions are declared in <TT>`stdlib.h'</TT>.<P><A NAME="IDX1338"></A><U>Function:</U> long int <B>strtol</B> <I>(const char *<VAR>string</VAR>, char **<VAR>tailptr</VAR>, int <VAR>base</VAR>)</I><P>The <CODE>strtol</CODE> ("string-to-long") function converts the initialpart of <VAR>string</VAR> to a signed integer, which is returned as a valueof type <CODE>long int</CODE>. <P>This function attempts to decompose <VAR>string</VAR> as follows:<P><UL><LI>A (possibly empty) sequence of whitespace characters. Which charactersare whitespace is determined by the <CODE>isspace</CODE> function(see section <A HREF="library_4.html#SEC55" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_4.html#SEC55">Classification of Characters</A>). These are discarded.<P><LI>An optional plus or minus sign (<SAMP>`+'</SAMP> or <SAMP>`-'</SAMP>).<P><LI>A nonempty sequence of digits in the radix specified by <VAR>base</VAR>.<P>If <VAR>base</VAR> is zero, decimal radix is assumed unless the series ofdigits begins with <SAMP>`0'</SAMP> (specifying octal radix), or <SAMP>`0x'</SAMP> or<SAMP>`0X'</SAMP> (specifying hexadecimal radix); in other words, the samesyntax used for integer constants in C.<P>Otherwise <VAR>base</VAR> must have a value between <CODE>2</CODE> and <CODE>35</CODE>.If <VAR>base</VAR> is <CODE>16</CODE>, the digits may optionally be preceeded by<SAMP>`0x'</SAMP> or <SAMP>`0X'</SAMP>.<P><LI>Any remaining characters in the string. If <VAR>tailptr</VAR> is not a nullpointer, <CODE>strtol</CODE> stores a pointer to this tail in<CODE>*<VAR>tailptr</VAR></CODE>.</UL><P>If the string is empty, contains only whitespace, or does not contain aninitial substring that has the expected syntax for an integer in thespecified <VAR>base</VAR>, no conversion is performed. In this case,<CODE>strtol</CODE> returns a value of zero and the value stored in<CODE>*<VAR>tailptr</VAR></CODE> is the value of <VAR>string</VAR>.<P>In a locale other than the standard <CODE>"C"</CODE> locale, this functionmay recognize additional implementation-dependent syntax.<P>If the string has valid syntax for an integer but the value is notrepresentable because of overflow, <CODE>strtol</CODE> returns either<CODE>LONG_MAX</CODE> or <CODE>LONG_MIN</CODE> (see section <A HREF="library_28.html#SEC486" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_28.html#SEC486">Range of an Integer Type</A>), asappropriate for the sign of the value. It also sets <CODE>errno</CODE>to <CODE>ERANGE</CODE> to indicate there was overflow.<P>There is an example at the end of this section.<P><A NAME="IDX1339"></A><U>Function:</U> unsigned long int <B>strtoul</B> <I>(const char *<VAR>string</VAR>, char **<VAR>tailptr</VAR>, int <VAR>base</VAR>)</I><P>The <CODE>strtoul</CODE> ("string-to-unsigned-long") function is like<CODE>strtol</CODE> except that it returns its value with type <CODE>unsignedlong int</CODE>. The value returned in case of overflow is <CODE>ULONG_MAX</CODE>(see section <A HREF="library_28.html#SEC486" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_28.html#SEC486">Range of an Integer Type</A>).<P><A NAME="IDX1340"></A><U>Function:</U> long int <B>atol</B> <I>(const char *<VAR>string</VAR>)</I><P>This function is similar to the <CODE>strtol</CODE> function with a <VAR>base</VAR>argument of <CODE>10</CODE>, except that it need not detect overflow errors.The <CODE>atol</CODE> function is provided mostly for compatibility withexisting code; using <CODE>strtol</CODE> is more robust.<P><A NAME="IDX1341"></A><U>Function:</U> int <B>atoi</B> <I>(const char *<VAR>string</VAR>)</I><P>This function is like <CODE>atol</CODE>, except that it returns an <CODE>int</CODE>value rather than <CODE>long int</CODE>. The <CODE>atoi</CODE> function is alsoconsidered obsolete; use <CODE>strtol</CODE> instead.<P>Here is a function which parses a string as a sequence of integers andreturns the sum of them:<P><PRE>sum_ints_from_string (char *string){ int sum = 0; while (1) { char *tail; int next; /* Skip whitespace by hand, to detect the end. */ while (isspace (*string)) string++; if (*string == 0) break; /* There is more nonwhitespace, */ /* so it ought to be another number. */ errno = 0; /* Parse it. */ next = strtol (string, &tail, 0); /* Add it in, if not overflow. */ if (errno) printf ("Overflow\n"); else sum += next; /* Advance past it. */ string = tail; } return sum;}</PRE><P><H3><A NAME="SEC308" HREF="library_toc.html#SEC308" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC308">Parsing of Floats</A></H3><A NAME="IDX1342"></A><P>These functions are declared in <TT>`stdlib.h'</TT>.<P><A NAME="IDX1343"></A><U>Function:</U> double <B>strtod</B> <I>(const char *<VAR>string</VAR>, char **<VAR>tailptr</VAR>)</I><P>The <CODE>strtod</CODE> ("string-to-double") function converts the initialpart of <VAR>string</VAR> to a floating-point number, which is returned as avalue of type <CODE>double</CODE>. <P>This function attempts to decompose <VAR>string</VAR> as follows:<P><UL><LI>A (possibly empty) sequence of whitespace characters. Which charactersare whitespace is determined by the <CODE>isspace</CODE> function(see section <A HREF="library_4.html#SEC55" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_4.html#SEC55">Classification of Characters</A>). These are discarded.<P><LI>An optional plus or minus sign (<SAMP>`+'</SAMP> or <SAMP>`-'</SAMP>).<P><LI>A nonempty sequence of digits optionally containing a decimal-pointcharacter--normally <SAMP>`.'</SAMP>, but it depends on the locale(see section <A HREF="library_7.html#SEC82" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_7.html#SEC82">Numeric Formatting</A>).<P><LI>An optional exponent part, consisting of a character <SAMP>`e'</SAMP> or<SAMP>`E'</SAMP>, an optional sign, and a sequence of digits.<P><LI>Any remaining characters in the string. If <VAR>tailptr</VAR> is not a nullpointer, a pointer to this tail of the string is stored in<CODE>*<VAR>tailptr</VAR></CODE>.</UL><P>If the string is empty, contains only whitespace, or does not contain aninitial substring that has the expected syntax for a floating-pointnumber, no conversion is performed. In this case, <CODE>strtod</CODE> returnsa value of zero and the value returned in <CODE>*<VAR>tailptr</VAR></CODE> is thevalue of <VAR>string</VAR>.<P>In a locale other than the standard <CODE>"C"</CODE> locale, this function mayrecognize additional locale-dependent syntax.<P>If the string has valid syntax for a floating-point number but the valueis not representable because of overflow, <CODE>strtod</CODE> returns eitherpositive or negative <CODE>HUGE_VAL</CODE> (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>), depending onthe sign of the value. Similarly, if the value is not representablebecause of underflow, <CODE>strtod</CODE> returns zero. It also sets <CODE>errno</CODE>to <CODE>ERANGE</CODE> if there was overflow or underflow.<P><A NAME="IDX1344"></A><U>Function:</U> double <B>atof</B> <I>(const char *<VAR>string</VAR>)</I><P>This function is similar to the <CODE>strtod</CODE> function, except that itneed not detect overflow and underflow errors. The <CODE>atof</CODE> functionis provided mostly for compatibility with existing code; using<CODE>strtod</CODE> is more robust.<P><P>Go to the <A HREF="library_17.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_17.html">previous</A>, <A HREF="library_19.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_19.html">next</A> section.<P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -