Input/Output Formatting in C


The main input routine in C programs is scanf and the main output routine is printf, both of which use a format string to control their behavior. The following program listing shows some common uses:
#include <stdio.h>

int main()
{
	int n;
	float f;
	double d;
	char s[100];

	/* input an integer */
	scanf("%d", &n);
	/* print an integer, no formatting */
	printf("%d\n", n);
	/* print an integer, padded on left with spaces to total 6 chars */
	printf("%6d\n", n);
	/* print an integer, padded on right with spaces to total 6 chars */
	printf("%-6d\n", n);
	/* print an integer, padded on left with zeroes to total 6 chars */
	printf("%.6d\n", n);

	/* input a string (whitespace delineated) */
	scanf("%s", s);
	/* print a string, no formatting */
	printf("%s\n", s);
	/* print a string, padded with spaces on left to 20 chars */
	printf("%20s\n", s);
	/* print a string, padded with spaces on right to 20 chars */
	printf("%-20s\n", s);
	/* print a string, truncated after 3 chars */
	printf("%.3s\n", s);

	/* input a single precision floating point number */
	scanf("%f", &f);
	/* print a float, default precision is 6 places */
	printf("%f\n", f);
	/* input a double precision floating point number */
	scanf("%lf", &d);
	/* print a double, default precision is 6 places */
	printf("%f\n", d);
	/* print a double, 2 places of precision */
	printf("%.2f\n", d);
	/* print a double, 2 places of precision, padded with space to 10 */
	printf("%10.2f\n", d);
	/* print a double, use exponential notation if more than 3 digits */
	printf("%.3g\n", d);
}

Sometimes it is useful when reading input to read into a line buffer using fgets(char * buf, int n, stdin) (where n is the size of the buffer) so that only one line is read (the newline is discarded). Then sscanf(buf, "format", ...) can be used to read the line into variables.

Last updated 2001/10/08