Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Output with printf: Mastering Formatting

Lesson 9 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

The Art of Displaying Data

The printf function (print formatted) is one of the most powerful tools in C. It doesn't just print text; it allows you to construct beautiful, aligned, and precise output for your users.

The Anatomy of printf

printf("Format String", variable1, variable2, ...); The "Format String" contains plain text mixed with "Format Specifiers" that act as placeholders for your variables.

Common Specifiers

  • %d or %i: Signed decimal integer.
  • %f: Floating point (decimal).
  • %lf: Double (long float).
  • %c: Single character.
  • %s: String (character array).
  • %p: Memory address (Pointer).
  • %x: Hexadecimal number.

Advanced Formatting Secrets

You can put flags and numbers between the % and the letter to control exactly how the data looks:

  1. Field Width (%10d): Specifies the minimum number of characters to print. If the number is shorter, it pads with spaces. Great for aligning columns in a table.
  2. Left Alignment (%-10d): The minus sign makes the output left-aligned within its field width.
  3. Precision (%.2f): Controls the number of decimal places for floats. %.2f will round 3.14159 to 3.14.
  4. Zero Padding (%05d): Pads the number with leading zeros (e.g., 7 becomes 00007).
  5. Sign Flag (%+d): Forces the display of a + sign for positive numbers.

Escape Sequences (Special Characters)

These allow you to print characters that you can't normally type in a string:

  • \n: Newline (Move to the next line).
  • \t: Horizontal Tab (Indent).
  • \b: Backspace.
  • \\: Print a literal backslash.
  • \": Print a literal double quote.
  • \a: Alert (Makes a "ding" sound on most computers!).
  • \r: Carriage Return (Moves the cursor back to the start of the current line).

Alignment Example

printf("%-15s %10s\n", "Item", "Price");
printf("-----------------------------\n");
printf("%-15s %10.2f\n", "Laptop", 999.99);
printf("%-15s %10.2f\n", "Mouse", 25.50);
printf actually returns a value! It returns the total number of characters it successfully printed to the screen. This is rarely used but can be useful for advanced debugging.