qc.hlp (Table of Contents; Topic list)
Important Notice
The pages on this site contain documentation for very old MS-DOS software, purely for historical purposes. If you're looking for up-to-date documentation, particularly for programming, you should not rely on the information found here, as it will be woefully out of date.
MKFPSTR.C
                                             Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
/* MKFPSTR.C illustrates building and displaying floating point strings
 * without printf. Functions illustrated include:
 *      strcat          strncat         cscanf          fcvt
 */
 
#include <stdlib.h>
#include <conio.h>
#include <string.h>
 
void main()
{
    int decimal, sign;
    int precision = 7;
    char *pnumstr, numbuf[50] = "", tmpbuf[50];
    double number1, number2;
 
    cputs( "Enter two floating point numbers: " );
    cscanf( "%lf %lf", &number1, &number2 );
    putch( '\n' );
 
    /* Use information from fcvt to format number string. */
    pnumstr = fcvt( number1 + number2, precision, &decimal, &sign );
 
    /* Start with sign if negative. */
    if( sign )
        strcat( numbuf, "-" );
 
    if( decimal <= 0 )
    {
        /* If decimal is to the left of first digit (decimal negative),
         * put in leading zeros, then add digits.
         */
        strcat( numbuf, "0." );
        memset( tmpbuf, '0', (size_t)abs( decimal ) );
        tmpbuf[abs( decimal )] = 0;
        strcat( numbuf, tmpbuf );
        strcat( numbuf, pnumstr );
    }
    else
    {
        /* If decimal is to the right of first digit, put in leading
         * digits. Then add decimal and trailing digits.
         */
        strncat( numbuf, pnumstr, (size_t)decimal );
        strcat( numbuf, "." );
        strcat( numbuf, pnumstr + decimal );
    }
    cputs( strcat( "Total is: ", numbuf ) );
}