/* * ===================================================================== * sscanf.c -- Demonstrate use of sscanf() and sprintf() * ===================================================================== * * Copyright (C) 1993-96 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written By: David Mazzoni June 1995 * ===================================================================== */ #include "miscellaneous.h" #include #include #include enum { MaxBuf = 256 }; /* size of auto string buffer */ /* * ================================================= * Convert a float to a dynamically allocated string * ================================================= */ char * ftoa_( float fVal ) { char caBuf[MaxBuf]; /* for conversion */ char* cpVal; /* for final, dynamic value */ sprintf( caBuf, "%f", fVal ); cpVal = safeMalloc( strlen( caBuf )+1, __FILE__, __LINE__ ); strcpy( cpVal, caBuf ); return cpVal; } /* * ================================================= * Convert a string of chars into a float value * ================================================= */ float atof_( char* cpFloatString ) { float fVal; sscanf( cpFloatString, "%f", &fVal ); return fVal; } /* * ======================================= * Test program for sscanf() and sprintf() * ======================================= */ int main( void ) { float fFloat = 1.2345e3F; char* cpString = "1.2345e3F"; printf( "Convert string %s to float --> %6.2f\n", cpString, atof_( cpString ) ); printf( "Convert float %6.2f to string --> %s\n", fFloat, ftoa_( fFloat ) ); return 0; }