Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. C Language Dictionary
  3. printf() / fprintf() / sprintf()

printf() / fprintf() / sprintf()

Formats and outputs values using a format string. printf() writes to standard output (the screen), while fprintf() writes to a specified stream (such as a file or standard error). Both are declared in <stdio.h>.

Syntax

#include <stdio.h>

// Writes formatted output to standard output.
printf(format_string, arg1, arg2, ...);

// Writes formatted output to the specified stream.
fprintf(stream, format_string, arg1, arg2, ...);

// Writes formatted output to a string buffer.
sprintf(buffer, format_string, arg1, arg2, ...);

// Safe version — also takes a buffer size limit.
snprintf(buffer, size, format_string, arg1, arg2, ...);

Common Format Specifiers

SpecifierTypeDescription
%d / %iintOutputs an integer as a decimal number.
%uunsigned intOutputs an unsigned decimal integer.
%ffloat / doubleOutputs a floating-point number with a decimal point. Defaults to 6 decimal places.
%e / %Efloat / doubleOutputs a number in scientific notation (e.g., 1.23e+02).
%g / %Gfloat / doubleOutputs using whichever of %f or %e produces the shorter result.
%ccharOutputs a single character.
%schar *Outputs a string up to the null terminator.
%pvoid *Outputs a pointer address in hexadecimal.
%x / %Xunsigned intOutputs an integer in hexadecimal.
%ounsigned intOutputs an integer in octal.
%zusize_tOutputs an unsigned size type, such as the return value of sizeof.
%%Outputs a literal % character.

Sample Code

#include <stdio.h>

int main(void) {
    // Examples of basic format specifiers.
    int    n = 42;
    double d = 3.14159;
    char   c = 'A';
    char  *s = "Hello";

    printf("Integer: %d\n",   n); // Outputs: "Integer: 42"
    printf("Float: %f\n",     d); // Outputs: "Float: 3.141590"
    printf("Char: %c\n",      c); // Outputs: "Char: A"
    printf("String: %s\n",    s); // Outputs: "String: Hello"

    // Flags and width specifiers.
    printf("[%10d]\n",  n); // Right-aligned (width 10): "[        42]"
    printf("[%-10d]\n", n); // Left-aligned (width 10):  "[42        ]"
    printf("[%010d]\n", n); // Zero-padded (width 10):   "[0000000042]"

    // Precision specifiers.
    printf("%.2f\n",  d);    // 2 decimal places: "3.14"
    printf("%.5s\n",  s);    // Truncate string to 5 chars: "Hello"
    printf("%8.2f\n", d);    // Width 8, 2 decimal places: "    3.14"

    // Use fprintf to write an error message to standard error.
    fprintf(stderr, "An error occurred.\n");

    // Use snprintf to write formatted output into a string buffer.
    char buf[64];
    snprintf(buf, sizeof(buf), "Score: %d points", 98);
    printf("%s\n", buf); // Outputs: "Score: 98 points"

    // Hexadecimal and octal output.
    printf("Hex (lower): %x\n",  255); // Outputs: "ff"
    printf("Hex (upper): %X\n",  255); // Outputs: "FF"
    printf("Octal: %o\n",        255); // Outputs: "377"
    printf("0x prefix: %#x\n",   255); // Outputs: "0xff"

    return 0;
}

Notes

The general format of a format specifier is %[flags][width][.precision]type. Available flags include left-align (-), zero-pad (0), and always show sign (+).

If a format specifier does not match the type of the corresponding argument, undefined behavior occurs. For example, using %d with a floating-point value will produce incorrect output. Enable compiler warnings (e.g., the -Wall option in gcc) to catch type mismatches.

For formatted input (reading values with a format string), see scanf() / fscanf(). For character-by-character I/O, see putchar() / getchar().

If you find any errors or copyright issues, please .