---
title: "Output of printf statements"
subject: "Computer Programming"
chapter: 4
key: "semester_1/computer_programming:printf-output-a-b"
source: https://www.bctengineer.com/q/semester_1/computer_programming/printf-output-a-b
site: BCT Engineering
---

# Output of printf statements

Computer Programming · Chapter 4: Input and Output

- Asked in: 2074 Chaitra, 4 marks
- 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/printf-output-a-b
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-4

## Question

Write down the output of printf() function for the following sections of statements.
```
float a = 5.7891;
int b = 6789;
printf("a=%4f and b=%-7d from first line",a,b);
printf("a=%-7.2f and b=%07d from second line",a,b);
printf("a=%.2f and b=%2d from third line",a,b);
```

## Short answer

The three printf calls produce one line: a=5.789100 and b=6789 from first linea=5.79 and b=0006789 from second linea=5.79 and b=6789 from third line.

## Answer

Running the statements (the calls have no newline, so everything appears on one line):

```c
#include <stdio.h>
int main(void)
{
    float a = 5.7891;
    int b = 6789;
    printf("a=%4f and b=%-7d from first line",a,b);
    printf("a=%-7.2f and b=%07d from second line",a,b);
    printf("a=%.2f and b=%2d from third line",a,b);
    return 0;
}
```
Output:
```text
a=5.789100 and b=6789    from first linea=5.79    and b=0006789 from second linea=5.79 and b=6789 from third line
```

#### Reason
- `%4f`: width 4 is smaller than needed, and the default precision is 6 decimals, so `5.789100`. `%-7d` prints 6789 left aligned in 7 columns (3 trailing blanks).
- `%-7.2f`: 2 decimals (5.79, rounded from 5.7891), left aligned in width 7 (3 trailing blanks). `%07d` pads with zeros to width 7: `0006789`.
- `%.2f` gives `5.79`; `%2d` has width 2, but 6789 has 4 digits, so it prints in full as `6789`.

## Check yourself

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

1. What does %4f print for 5.7891?
   A. 5.789
   B. 5.7891
   C. 5.789100
   D. 5.79
   Answer: C. Default precision is 6 decimals; the width 4 is smaller than needed. (Key point: %4f)

2. What does %-7d do with 6789?
   A. Right aligns in 7 columns
   B. Left aligns in 7 columns (3 trailing blanks)
   C. Zero pads
   D. Prints -6789
   Answer: B. The minus flag left aligns; 4 digits plus 3 blanks. (Key point: %-7d)

## Related in Computer Programming

- [Correct program: case array and average](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-case-array) — Computer Programming, chapter 3
- [Output of arithmetic expression program](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aoutput-expression-xyz) — Computer Programming, chapter 3
- [Pass 2D array; largest and smallest](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apass-2d-array-largest-smallest) — Computer Programming, chapter 7
- [Convert case based on first character](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswap-case-first-char-word) — Computer Programming, chapter 7
- [Pattern: PULCHOWK using unformatted output](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apattern-pulchowk) — Computer Programming, chapter 4
- [Significance of main() function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-of-main) — Computer Programming, chapter 6

## Related in other subjects

- [Cube of int, float, double by overloading](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Acube-overloaded-program) — Object Oriented Programming, chapter 3
- [Second law statements; Kelvin-Planck and Clausius equivalence](https://www.bctengineer.com/question.md?key=semester_2%2Ffundamental_of_thermodynamics_and_heat_transfer%3Asecond-law-classical-kelvin-clausius) — Fundamental of Thermodynamics & Heat Transfer, chapter 5
- [Cascade of first-order sections for 12/(s²+8s+12)](https://www.bctengineer.com/question.md?key=semester_5%2Ffilter_design%3Acascade-first-order-12-over-s2) — Filter Design, chapter 6, asked 2×
- [First and second order finite difference schemes](https://www.bctengineer.com/question.md?key=semester_8%2Fcomputational_techniques_in_civil_engineering%3Afd-first-second-order-schemes) — Computational Techniques in Civil Engineering, chapter 5, asked 2×
