Node:I/O of Rationals, Previous:Applying Integer Functions, Up:Rational Number Functions



Input and Output Functions

When using any of these functions, it's a good idea to include stdio.h before gmp.h, since that will allow gmp.h to define prototypes for these functions.

Passing a NULL pointer for a stream argument to any of these functions will make them read from stdin and write to stdout, respectively.

size_t mpq_out_str (FILE *stream, int base, mpq_t op) Function
Output op on stdio stream stream, as a string of digits in base base. The base may vary from 2 to 36. Output is in the form num/den or if the denominator is 1 then just num.

Return the number of bytes written, or if an error occurred, return 0.

size_t mpq_inp_str (mpq_t rop, FILE *stream, int base) Function
Read a string of digits from stream and convert them to a rational in rop. Any initial white-space characters are read and discarded. Return the number of characters read (including white space), or 0 if a rational could not be read.

The input can be a fraction like 17/63 or just an integer like 123. Reading stops at the first character not in this form, and white space is not permitted within the string. If the input might not be in canonical form, then mpq_canonicalize must be called (see Rational Number Functions).

The base can be between 2 and 36, or can be 0 in which case the leading characters of the string determine the base, 0x or 0X for hexadecimal, 0 for octal, or decimal otherwise. The leading characters are examined separately for the numerator and denominator of a fraction, so for instance 0x10/11 is 16/11, whereas 0x10/0x11 is 16/17.