scanf.c

/*
 * SCANF -- shows how to read using a format
 *
 * Usage: scanf (or ./scanf)
 *
 * Inputs: You are prompted for these
 * Outputs: various formatted values
 * Exit Code: 0 because all goes well
 *
 * IMPORTANT NOTE:
 * When you compile this with gcc and -Wall, you get
 * warnings about bad printfs; those are there for
 * demonstration
 *
 * written for ECS 36A, Fall 2019
 * modified for ECS 36A, Spring 2025 to fix incompatibilities with earlier version
 *
 * Matt Bishop, Oct. 11, 2019
 * 	original program written
 * Matt Bishop, Apr. 6m,2025
 *	 modified for C11 to fix incompatibilities with earlier version of C
 */
#include <stdio.h>

int main(void)
{
	int i, j;	/* variables for input */
	int r;		/* return value of scanf */
	int k;		/* used to break out of a while loop */
	int c;		/* used to read a line */
	char x;		/* variable for input */

	/*
	 * read in an integer
	 */
	printf("enter an integer: ");
	if ((r = scanf("%d", &i)) == 1)
		printf("You typed '%d'\n", i);
	else if (r == EOF){
		printf("You typed EOF to end input\n");
		return(0);
	}
	else
		printf("You didn't type an integer\n");

	/*
	 * now read 2 integers
	 */
	printf("enter 2 integers: ");
	if ((r = scanf("%d %d", &i, &j)) == 2)
		printf("You typed '%d %d'\n", i, j);
	else if (r == 1)
		printf("You typed '%d' and a non-integer\n", i);
	else if (r == EOF){
		printf("You typed EOF to end input\n");
		return(0);
	}
	else
		printf("You didn't type an integer\n");

	/*
	 * here's a good way to mess up
	 */
	printf("enter a string that doesn't begin with a number: ");
	k = 100;
	while (k > 0 && scanf("%d %d", &i, &j) != 2){
		if (k % 10 == 0)
			printf("Going around for the %d-th time\n", k);
		k = k - 1;
	}
	printf("What happened is it tries to read another. But it sees a non-digit\n");
	printf("Bnd so does not read it, and returns -1. But it then goes back (the\n");
	printf("while loop) and repeats this. Were it not for the k this would go\n");
	printf("on indefinitely. So *always* check that the return value of scanf is\n");
	printf("EOF or what you expect.\n");

	/*
	 * Here's an idiom to clean out the unread part of the line
	 */
	while ((c = getchar()) != '\n' && c != EOF)
		printf("read char '%c'\n", c);

	/*
	 * now we can read an integer
	 */
	printf("enter an integer: ");
	if ((r = scanf("%d", &i)) == 1)
		printf("You typed '%d'\n", i);
	else if (r == EOF){
		printf("You typed EOF to end input\n");
		return(0);
	}

	/* 
	 * now we read a character
	 */
	printf("Now we read the character following the integer you typed above\n");
	printf("If we didn't type anything else, just hit return or enter, it\n");
	printf("that's what is read and printed; if it's any other  character,\n");
	printf("the rest of the line is discarded; if you typed EOF, then every\n");
	printf("future scanf in the program returns EOF.\n");
	if ((r = scanf("%c", &x)) == 1)
		printf("You typed '%c'\n", x);
	else if (r == EOF){
		printf("You typed EOF to end input\n");
		return(0);
	}
	else
		printf("You didn't type an integer\n");
	

	return(0);
}


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