---
title: "Syntax and example of printf()"
subject: "Computer Programming"
chapter: 4
key: "semester_1/computer_programming:syntax-printf"
source: https://www.bctengineer.com/q/semester_1/computer_programming/syntax-printf
site: BCT Engineering
---

# Syntax and example of printf()

Computer Programming · Chapter 4: Input and Output

- Asked in: 2071 Shrawan, 1 marks; 2071 Chaitra, 1 marks (asked 2 times)
- Video: [Basic Output Function – printf](https://www.youtube.com/watch?v=VXol2-SoUy8) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/syntax-printf
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-4

## Question

Write 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.

## Answer

`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:**
```c
#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:
```text
Hello, World!
n = 25, f = 3.14, c = A
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. printf() is used to:
   A. Read input
   B. Display formatted output
   C. Open files
   D. Allocate memory
   Answer: B. printf writes formatted output to the screen. (Key point: Purpose)

2. What does \n do in a control string?
   A. Tab
   B. New line
   C. Backspace
   D. Null
   Answer: B. \n moves to the next line (\t is a tab). (Key point: Escape sequence)

3. Which specifier prints a float?
   A. %d
   B. %c
   C. %s
   D. %f
   Answer: D. %f is for float, %d int, %c char, %s string. (Key point: Specifier)
