.TH getc() "" "" "STDIO Function (libc)"
.PC "Read character from file stream"
.B "#include <stdio.h>"
\fBint getc(\fIfp\^\fB)
FILE *\fIfp\^\fB;\fR
.PP
.B getc()
is a function that reads a character from the file stream \fIfp\fR,
and returns an
.BR int .
.SH Example
The following example creates a simple copy utility.
It opens the first file named on the command line and copies its contents
into the second file named on the command line.
.DM
#include <stdio.h>
.DE
.DM
void fatal(string)
char *string;
{
	printf("%s\en", string);
	exit (1);
}
.DE
.DM
main(argc, argv)
int argc; char *argv[];
{
	int foo;
	FILE *source, *dest;
.DE
.DM
	if (--argc != 2)
		fatal("Usage: copy [source][destination]");
.DE
.DM	
	if ((source = fopen(argv[1], "r")) == NULL)
		fatal("Cannot open source file");
	if ((dest = fopen(argv[2], "w")) == NULL)
		fatal("Cannot open destination file");
.DE
.DM
	while ((foo = getc(source)) != EOF)
		putc(foo, dest);
}
.DE
.SH "See Also"
.Xr "fgetc()," fgetc
.Xr "getchar()," getchar
.Xr "libc," libc
.Xr "putc()" putc
.br
\*(AS, \(sc7.9.7.5
.br
\*(PX Standard, \(sc8.1
.SH Diagnostics
.B getc()
returns EOF at end of file or on read fatal.
.SH Notes
Because
.B getc()
is a macro, arguments with side effects probably will not work as expected.
Also,
because
.B getc()
is a complex macro,
its use in expressions of too great a complexity may
cause unforeseen difficulties.
Use of the function
.B fgetc()
may avoid this.
