fibdriver.c

/*
 * FIBDRIVER -- program to call a Fibonacci printing function
 *
 * Compiling: put the Fibonacci function in fib.c, then say:
 * 	gcc -ansi -pedantic -Wall -o fib fibdriver.c fib.c
 *
 * Usage: fib`
 *
 * Inputs (from standard input)
 *      -- a non-negative integer
 * Outputs (to standard output)
 * 	-- a single line of the first n Fibonacci numbers, separated by
 * 	   blanks
 * Errors (to standard error)
 * 	-- "Need an integer\n" -- input isn't an EOF or integer
 * 	-- "Cannot print a negative number of Fibonacci numbers\n" --
 * 		input is a negative integer
 *
 * Exit Code: 0 if all is well, 1 on error
 *
 * written for ECS 030, Fall 2015
 * 
 * Matt Bishop, Oct. 11, 2015
 *      original program written
 */
#include <stdio.h>
#include <stdlib.h>

/*
 *  prototypes
 */
int fib(int);		/* function to print Fibonacci numbers */


/*
 * the main routine -- start here
 */
int main(void)
{
	int count;	/* number of Fibonacci numbers to print */
	int nread;	/* return value of reading */

	/*
	 * read the number -- we hope! Handle errors or EOF
	 */
	if ((nread = scanf("%d", &count)) == EOF)
		return(EXIT_SUCCESS);
	else if (nread != 1){
		/* oops ... not an integer */
		fprintf(stderr, "Need an integer\n");
		return(EXIT_FAILURE);
	}

	/*
	 * got a number -- fib checks its validity
	 * if we skip the if body, all went well
	 * otherwise, not
	 */
	if (!fib(count)){
		fprintf(stderr,
		      "Cannot print a negative number of Fibonacci numbers\n");
		return(EXIT_FAILURE);
	}

	/*
	 * say goodnight, Dick!
	 */
	return(EXIT_SUCCESS);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
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