HP C/iX Library Reference Manual (30026-90004)
Chapter 5 193
HP C/iX Library Function Descriptions
getc
getc
Reads a character from an open stream.
Syntax
#include <stdio.h>
int getc (FILE *
stream
);
Parameters
stream
A pointer to an open stream.
Return Values
x The character read, expressed as an integer.
EOF No more data, or an error occurred.
Description
The getc function returns the next character from the input stream pointed to by
stream
.
The getc function is equivalent to the fgetc function except that it is implemented as a
macro. Because getc() can evaluate the stream more than once, the arguments should
never be an expression with side effects.
Examples
A simple version of a command to print a file can be written using getc() and putc():
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[ ];
{
int c;
FILE *fp;
if(argc != 2) {
printf("Usage: cat file\n");
exit(1);
}
fp = fopen(argv[1], "r");
if(fp == NULL) {
printf("Can't open %s.\n", argv[1]);
exit(1);
}
while((c = getc(fp)) != EOF)
putc(c, stdout);