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.
ATONUM.C
                                             Up Contents Index Back
────────────────────────────────────────────────────────────────────────────
 
/* ATONUM.C illustrates string to number conversion functions including:
 *      atof            atoi            atol            gcvt
 *
 * It also illustrates:
 *      cgets           cputs
 */
 
#include <stdlib.h>
#include <string.h>
#include <conio.h>
 
#define MAXSTR 100
 
char cnumbuf[MAXSTR] = { MAXSTR + 2, 0 };
char tmpbuf[MAXSTR];
 
/* Numeric input and output without printf */
void main()
{
    int     integer;
    long    longint;
    float   real;
    char    *numbuf;
 
    /* Using cgets (rather than gets) allows use of DOS editing keys
     * (or of editing keys from DOS command-line editors).
     */
    cputs( "Enter an integer: " );
    numbuf = cgets( cnumbuf );
    cputs( "\r\n" );                /* cputs doesn't translate \n     */
    integer = atoi( numbuf );
    strcpy( tmpbuf, numbuf );
    strcat( tmpbuf, " + " );
 
    cputs( "Enter a long integer: " );
    numbuf = cgets( cnumbuf );
    cputs( "\r\n" );
    longint = atol( numbuf );
    strcat( tmpbuf, numbuf );
    strcat( tmpbuf, " + " );
 
    cputs( "Enter a floating point number: " );
    numbuf = cgets( cnumbuf );
    cputs( "\r\n" );
    real = (float)atof( numbuf );
    strcat( tmpbuf, numbuf );
    strcat( tmpbuf, " = " );
 
    gcvt( integer + longint + real, 4, numbuf );
    strcat( tmpbuf, numbuf );
    strcat( tmpbuf, "\r\n" );
 
    cputs( tmpbuf );
}