Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

printf: formatting standard output

printf, declared in stdio.h, writes formatted text to stdout. Format specifiers must match the actual argument types passed to the function.

Version: C23

Syntax

int printf(const char *restrict format, ...);

The first argument is a format string. Ordinary characters go to output, while specifiers such as %d, %s, and %f describe how subsequent arguments are retrieved and written. %% writes a percent sign without consuming an argument. The function returns the number of characters written, or a negative value on an output or encoding error.

%d expects an int, %s a pointer to a valid null-terminated string, and %zu a size_t value. %f takes a double; a float argument is promoted to double when passed to a variadic function. A mismatch between specifier and argument can cause undefined behavior.

Do not use user input as the format: write printf("%s", text) rather than printf(text). Width and precision modifiers control formatting but do not replace pointer validation. \n denotes a newline in a C string; standard-output buffering is a separate concern.

Examples

#include <stdio.h>

int main(void) {
    printf("%s %d %.1f%%\n", "CKTIS", 7, 12.5);
    return 0;
}

Result

CKTIS 7 12.5%

Sources