Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <stdio.h>

int puts(const char *s);
int fputs(const char *restrict s, FILE *restrict stream);

Try it

C The newline difference

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.

C Writing text to a file

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

C
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

  • puts appends \n, fputs does not. Forgetting this is the most common mistake: fputs("Name: ", stdout) leaves the cursor on the same line, which is useful for prompts.
  • fputs does not flush. A prompt written with fputs("Name: ", stdout) may not appear before you read input when stdout is line-buffered and there is no newline; call fflush(stdout) (on a terminal, reading from stdin usually flushes stdout for you, but this is not guaranteed).
  • Prefer them to printf("%s\n", s) for fixed text, and always over printf(s) when s could contain a %. A compiler may even translate printf("text\n") into a puts call.
  • 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() and putc() (or fputc); fputc('\n', f) is the usual way to add the newline manually.
  • Wide characters. fputws in <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 fflush if the sequence of messages on one terminal matters.
  • Do not confuse with gets. The input function gets was removed from the language; the safe reading function is fgets(), which keeps the newline where puts adds one.
  • Complexity: linear in the string length.