PUTC(3S)
NAME
putc, putchar, fputc, putw − put character or word on a stream
SYNOPSIS
#include <stdio.h>
int putc (c, stream)
int c;
FILE ∗stream;
int putchar (c)
int c;
int fputc (c, stream)
int c;
FILE ∗stream;
int putw (w, stream)
int w;
FILE ∗stream;
DESCRIPTION
Putc writes the character c onto the output stream at the position where the file pointer, if defined, is pointing. Putchar(c) is defined as putc(c, stdout). Putc and putchar are defined as both macros and functions.
Fputc behaves like putc, but is a function rather than a macro; it may therefore be used as an argument. Fputc runs more slowly than putc, but it takes less space per invocation and its name can be passed as an argument to a function.
Putw writes the word (i.e., int in C) w to the output stream (at the position at which the file pointer, if defined, is pointing). The size of a word is the size of an integer and varies from machine to machine. Putw neither assumes nor causes special alignment in the file.
Output streams, with the exception of the standard error stream stderr, are by default buffered if the output refers to a file and line-buffered if the output refers to a terminal. The standard error output stream, stderr, is by default unbuffered, but use of freopen (see fopen(3S)) will cause it to become buffered or line-buffered. Setbuf(3S) or setvbuf (see setbuf(3S)) may be used to change the stream’s buffering strategy.
RETURN VALUE
On success, putc, fputc, and putchar each return the value they have written. On failure, they return the constant EOF. This will occur if the file stream is not open for writing or if the output file cannot be grown. The function putw returns non-zero when an error has occurred; otherwise the function returns 0.
WARNINGS
The putc and putchar routines are implemented as both library functions and macros. The macro versions, which are used by default, are defined in <stdio.h>. To obtain the library function either use a #undef to remove the macro definition or, if compiling in ANSI-C mode, enclose the function name in parenthesis or use the function address. For following example illustrates each of these methods :
#include <stdio.h>
#undef putc
...
main()
{
int (*put_char()) ();
...
return_val=putc(c,fd);
...
return_val=(putc)(c,fd1);
...
put_char = putchar;
};
Line buffering may cause confusion or malfunctioning of programs that use standard I/O routines but use read(2) themselves to read from standard input. When a large amount of computation is done after printing part of a line on an output terminal, it is necessary to fflush (on fclose(3S)) the standard output before beginning the computation.
The macro version of putc incorrectly treats the argument stream with side effects. In particular, the followng call may not work as expected:
putc(c, ∗f++);
The function version of putc or fputc should be used instead.
Because of possible differences in word length and byte ordering, files written using putw are machine-dependent, and may not be read using getw on a different processor.
SEE ALSO
fclose(3S), ferror(3S), fopen(3S), getc(3S), fread(3S), printf(3S), puts(3S), setbuf(3S).
STANDARDS CONFORMANCE
putc: SVID2, XPG2, XPG3, POSIX.1, FIPS 151-1, ANSI C
fputc: SVID2, XPG2, XPG3, POSIX.1, FIPS 151-1, ANSI C
putchar: SVID2, XPG2, XPG3, POSIX.1, FIPS 151-1, ANSI C
putw: SVID2, XPG2, XPG3
Hewlett-Packard Company — HP-UX Release 7.0: Sept 1989