conv1.c

/*
 * program to show how atol/atof work
 * enter number when prompted
 *
 * Matt Bishop, ECS 36A
 *
 * April 30, 2024
 *	-- original version
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * Macros
 */
#define BUFFERSIZE	1024	/* size of input buffer */

/*
 * main routine
 */
int main(void)
{
	char buf[BUFFERSIZE];	/* input buffer */
	long i;			/* integer read in */
	double f;		/* double read in */

	/*
	 * read in a line and process it
	 */
	printf("How atol/atof work:\n> ");
	while(fgets(buf, BUFFERSIZE, stdin) != NULL){
		/* clobber the trailing newline to keep the output clean */
		if (buf[strlen(buf)-1] == '\n')
			buf[strlen(buf)-1] = '\0';
		/* convert to integer and print it */
		i = atol(buf);
		printf("\'%s\' is the integer %ld\n", buf, i);
		/* convert to double and print it */
		f = atof(buf);
		printf("\'%s\' is the double  %f\n", buf, f);
		printf("> ");
	}
	/* clean up the end of the output */
	putchar('\n');

	/*
	 * done!
	 */
	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: [email protected]
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh