Skip to main content

Syntax and example of printf()

Short answer

printf() outputs formatted text to stdout. Syntax: printf("control string", argument_list); where control string includes text, format specifiers (%d, %f) and escape sequences (\n). Example: printf("n = %d, f = %.2f, c = %c\n", 25, 3.14159f, 'A'); prints n = 25, f = 3.14, c = A.

Write syntax and example of printf().

printf() displays formatted output on the screen (standard output). It is declared in <stdio.h>.

Syntax: printf("control string", argument_list);

The control string contains text, format specifiers (%d, %f, %c, %s) and escape sequences (\n new line, \t tab). Each specifier is replaced by the corresponding argument.

Example:

#include <stdio.h>
int main(void)
{
    int n = 25;
    float f = 3.14159f;
    char c = 'A';
    printf("Hello, World!\n");
    printf("n = %d, f = %.2f, c = %c\n", n, f, c);
    return 0;
}

Output:

Hello, World!
n = 25, f = 3.14, c = A

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 4 questions →