• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C » Variables » How to Formatted Output using the printf Function
Next →
← Prev

How to Formatted Output using the printf Function

By Dinesh Thakur

The printf function allows the values of argument expressions to be printed in various formats. A large number of conversion characters are provided for printing expressions of different types. Also, the possibility of using several optional fields along with these conversion characters makes printf a very powerful and complicated function.

For a value being printed in a printf statement, we can specify the minimum fieldwidth, precision, justification (left or right), etc. using the format given below.

%

Flag

(-)

Minimum

field width

.

Precision

  Conversion character

        (d f c s, etc.)

When minimum field-width is not specified in a conversion specification, the corresponding argument is printed using as many character positions as necessary. However, if it is specified, the argument is printed in a field at least that wide, using additional spaces as necessary. If the field-width specified is more than what is required, the output values are right justified in the output field, unless the ·- · flag is used which causes the values to be left justified. If the specified field-width is insufficient for the value being printed, additional character positions are used as necessary.

When printing floating point numbers, precision specifies the minimum number of digits to be printed after the decimal point. If a number being printed has fewer digits after the decimal point than specified, it is padded with zeros. However, if it has more digits after the decimal point, its value is rounded to the specified digit. Thus, the conversion specification %8.4f causes numbers 100.12 and 123.12345 to be printed as 100.1200 and 123.1235, respectively. Note that. if we omit precision in the conversion specification, a default precision of six digits is used. In this section, we use the precision field only for floating point numbers.

Example Formatted Output

a) Using minimum field-width and precision

Consider the printf statements given below that print value of different types by specifying · minimum field-width for each data value and the precision for the floating values:

printf (“%10d\n”, 12);

printf(“%10.7f %10.4f\n”, 1.23456789, -1.2);

printf(“%10s\n”, “Apple”);

printf(“%10c\n”, ‘A’);

The output of these statements is given below.

              12

       1.2345679     -1.2000

           Apple

              A

Observe that the values are printed right justified in field-width of 10. Also observe that the first floating number is rounded to the seventh decimal place, whereas the second one is printed using additional zeros.

b) Effect of specifying insufficient field-width

printf(“%4s %4d %4f\n”, “Monday”, 12345, 1.2);

This statement uses a field-width of four for each constant to be printed. Note that this fieldwidth is not sufficient for any of the constants, including 1.2, as the default precision for a floating point number is six digits. Thus, the values are printed using as many character positions as required. The output is given below.

Monday 12345 1.200000

c) Printing tabular output specifying field-width and alignment

This example illustrates how we can use the formatting features of printf function to print data properly aligned in a tabular manner.

printf(“%-10s %6d %12.3f %4c\n”, “Monday”, 5, 2.1234, ‘A’);

printf(“%-10s %6d %12.3f %4c\n”, “Tuesday”, 16, 17.5126, ‘B’);

printf(“%-10s %6d %12.3f %4c\n”, “Wednesday”, 175, 120.25, ‘C’);

This code has three similar printf statements, each used to print four values using an identical format. Note that the minimum field-widths specified in the conversion specifications are more than what is required. Thus, the values will be printed right justified unless the ‘ – ‘ flag is used.

As the strings are printed using “-10s” conversion specification, each string is printed left justified in a field-width of ten characters, whereas the values of other arguments are printed right justified in respective fields. The floating numbers are printed using three decimal places after the decimal point. The output is given below.

Monday            5          2.123                A

Tuesday          16         17.513                B

Wednesday       175        120.250                C

You’ll also like:

  1. Formatted Output – the printf Function
  2. Formatted Input – the scanf Function
  3. Function of printf and scanf in C
  4. What is the Return Value from Printf() Function
  5. Program to See if Printf() Rounds or Truncates
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C Programming

C Programming Tutorials

  • C - History
  • C - Anatomy
  • C - Constants
  • C - Identifiers
  • C - Data Types
  • C - Libraries File
  • C - Header Files
  • C - Basic Language
  • C - Data Types Sizes
  • C - Header Files Importance
  • C - Escape Sequences
  • C - Main() Purpose
  • C - Program Procedure
  • C - Control Statements
  • C - Enumeration Constant
  • C - Add numbers
  • C - Return Statement
  • C - Avoid Goto
  • C - Command Line Arguments
  • C - Switch Case
  • C - Switch Case Limitations
  • C - getchar() and putchar()
  • C - Iteration Statements
  • C - Pass by Value and Reference
  • C - Structures and Unions
  • C - Structure
  • C - Dynamic Memory
  • C - Fgets and Fputs Functions
  • C - Gets() and Puts() Functions
  • C - Armstrong Number
  • C - Storage Classes
  • C - Fibonacci Series
  • C - Precision Setting
  • C - const Parameters

C - Variable & It's Type

  • C - Variables
  • C - Variable Lifetime
  • C - Static Variable
  • C - Register Variable
  • C - Global Variables
  • C - Auto Variables
  • C - Local Variables

C - Operator & Expressions

  • C - Operator
  • C - Boolean Operators
  • C - Bitwise Operator
  • C - Arithmetic Operators
  • C - Modulus Operator
  • C - Ternary Operator
  • C - Expressions
  • C - Arithmetic Expressions

C - Array

  • C - Arrays
  • C - Array Types
  • C - Array Characteristics
  • C - Static Arrays
  • C - Global Arrays
  • C - 3D Arrays
  • C - Dynamic Arrays
  • C - Pointer to 3D Arrays
  • C - Array Elements Hold
  • C - Arrays as Function Parameters
  • C - Accessing Matrix Elements
  • C - File Handling
  • C - Matrix Multiplication
  • C - Dynamic Memory Allocation

C - Searching & Sorting

  • C - Data Structures
  • C - Linear Search
  • C - Bubble Sort
  • C - Merge Sort
  • C - Linked List
  • C - Insertion Sort
  • C - Binary Search
  • C - Selection Sort
  • C - Quick Sort

C - Functions

  • C - Functions
  • C - Functions Advantages
  • C - Void Functions
  • C - Function Call
  • C - Default Return Value
  • C - String functions

C - Pointer

  • C - Pointers
  • C - Type Casting Of Pointers
  • C - Pointer Advantages
  • C - Pointers Initialization
  • C - Vectors and Pointers

C - Differences

  • C - C Vs C++
  • C - Formal Args. Vs Actual Args.
  • C - Keywords Vs Identifiers
  • C - Strings Vs Character Arrays
  • C - Address Vs Dereference Operator
  • C - Goto Vs longjmp
  • C - Declaring Vs Defining Variable
  • C - String Vs Array
  • C - Call by Value Vs Reference
  • C - Structure Vs Union
  • C - For Vs While loops
  • C - Compiler Vs Interpreter

C - Programs

  • C Program Standard Deviation
  • C Program Calculate Tax
  • C Program Sum Series
  • C Program Merge Arrays
  • C Program Euclid’s Algorithm
  • C Program Print Weekdays
  • C Program Sum of Digits
  • C Program Print a list
  • C Program Print Pythagorean
  • C Program Quiz program
  • C Program Display Table
  • C Program Print Comma-Separated
  • C Program Prints Prime Numbers
  • C Program for Print Integer
  • C Program Count Number
  • C Program Print Color Name
  • C Program Print Odd Numbers
  • C Program Calculate area
  • C Program for a Menu
  • C Program Add Two Vectors
  • C Program Array Addresses
  • C Program Division by Zero Error
  • C Program Compare two Dates
  • C Program Tower of Hanoi
  • C Program return 3 Numbers
  • C Program for Prime Numbers
  • C Program for Factorial
  • C Program for Palindrome

Other Links

  • C Programming - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW