C Reference ยท stdio.h
C puts() and fputs() functions
Reference for C puts() and fputs(): write a string to stdout or any stream, the newline difference, return values and why they beat printf for plain text.
puts() writes a string to standard output and then adds a newline. fputs() writes a string to any stream and adds nothing. Neither interprets % characters, which makes them the right choice for printing plain text: there is no format string to get wrong and no risk from %n or %s sequences in the data.
Both stop at the string's terminating null character, which they do not write. The only difference that trips people up is the newline: puts("hi") and fputs("hi\n", stdout) produce the same output.
#include <stdio.h> int puts(const char *s); int fputs(const char *restrict s, FILE *restrict stream);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
int puts(const char *s); int fputs(const char *restrict s, FILE *restrict stream);
| Name | Description |
|---|---|
s |
A null-terminated string. The terminator itself is not written. |
stream |
An open, writable stream (stdout, stderr, or from fopen). |
Returns: a non-negative value on success (the exact value is unspecified; do not compare it to the length), or EOF on a write error (with the stream's error indicator set).
Errors / undefined behavior: a null pointer or a character array without a terminator is undefined behavior, as is a closed or read-only stream.
Usage notes
putsappends\n,fputsdoes not. Forgetting this is the most common mistake:fputs("Name: ", stdout)leaves the cursor on the same line, which is useful for prompts.fputsdoes not flush. A prompt written withfputs("Name: ", stdout)may not appear before you read input whenstdoutis line-buffered and there is no newline; callfflush(stdout)(on a terminal, reading fromstdinusually flushesstdoutfor you, but this is not guaranteed).- Prefer them to
printf("%s\n", s)for fixed text, and always overprintf(s)whenscould contain a%. A compiler may even translateprintf("text\n")into aputscall. - Embedded null characters end the string early, so these functions cannot write binary data. Use
fwrite()for bytes. - Partial output on error. When a stream error occurs, some characters may already have been written.
- Single characters are written with
putchar()andputc()(orfputc);fputc('\n', f)is the usual way to add the newline manually. - Wide characters.
fputwsin<wchar.h>is the wide counterpart; do not mix narrow and wide output on one stream. - Stdout and stderr ordering. Both are separate streams with different buffering. Use
fflushif the sequence of messages on one terminal matters. - Do not confuse with
gets. The input functiongetswas removed from the language; the safe reading function isfgets(), which keeps the newline whereputsadds one. - Complexity: linear in the string length.
Related
printf,putcharandputc,fgets,fprintfanddprintf,fflush- Lessons: Output, Text files