← Back to C++ Course | Chapter 2: Input & Output | Lesson 7 of 8

C++ की iomanip Library

iomanip library output को neatly line up करने के tools देती है, एक table में column widths set करने जैसा। आप pad, align कर सकते हैं, और decimal places चुन सकते हैं।
Syntax
cpp
#include <iomanip>

std::cout << std::setw(width) << value;

std::cout << std::setfill('fill_char') << std::setw(width) << value;

setw से Width Set करना

setw(n) print होने वाली अगली value के लिए n characters का एक fixed-width field reserve करता है, इसे spaces से pad करते हुए ताकि output के columns neatly line up हों।

यह सिर्फ अगले single output operation को affect करता है, इसलिए अगर आपको aligned columns वाली पूरी table चाहिए तो हर value से पहले इसे दोबारा call करना होगा।

उदाहरण: Setting Width with setw

cpp
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << std::setw(10) << "Name" << std::setw(10) << "Score" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

setfill से Fill Characters Set करना

setfill(c) बदलता है कि कौन सा character setw की छोड़ी empty space भरता है — default से spaces, लेकिन आप numbers को zero-pad करने के लिए 0 या एक visual separator के लिए '*' इस्तेमाल कर सकते हैं।

यह setw के उलट, जो सिर्फ एक बार apply होता है, आपके इसे दोबारा बदलने तक बाद की सारी setw calls के लिए effect में रहता है।

उदाहरण: Setting Fill Characters with setfill

cpp
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << std::setfill('0') << std::setw(5) << 42 << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Floating Point Precision

setprecision(n) cap करता है कि floating-point output के लिए कितने significant digits (या decimal places, fixed के साथ combined होने पर) दिखते हैं, जो 3.14159265358979 जैसे digits की लंबी, गंदी strings रोकता है।

यह जब भी आप एक user को averages या percentages जैसी computed values display कर रहे हों essential है।

उदाहरण: Floating Point Precision

cpp
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>

// Program execution starts in main()
int main() {
	// Declare average and set it to 3.14159265358979
	double average = 3.14159265358979;
	// Print to the console with cout
	std::cout << std::setprecision(4) << average << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Left और Right Alignment

left और right control करते हैं कि text एक setw-defined field के किस side से चिपके — left text को शुरुआत में push करता है padding बाद में trail करते हुए, जबकि right (default) text से पहले pad करता है ताकि यह field के right edge के against align हो।

इनके बीच switch करना ही आपको same table में left-aligned labels और right-aligned numbers दोनों बनाने देता है।

उदाहरण: Left and Right Alignment

cpp
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << std::left << std::setw(10) << "Name" << std::endl;
	// Print to the console with cout
	std::cout << std::right << std::setw(10) << "Score" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Fixed Point Notation

fixed को setprecision(2) के साथ combine करना floating-point output को actual value की precision की परवाह किए बिना exactly दो decimal places पर lock कर देता है, जो $19.99 जैसी currency display करने के लिए exactly वह format है जिसकी उम्मीद है।

fixed के बिना, अकेला setprecision specifically decimal places की बजाय total significant digits control करता है, जो 100.5 जैसी values के लिए unexpected results produce कर सकता है।

उदाहरण: Fixed Point Notation

cpp
// Include std::cout and std::cin
#include <iostream>
#include <iomanip>

// Program execution starts in main()
int main() {
	// Declare price and set it to 19.99
	double price = 19.99;
	// Print to the console with cout
	std::cout << "$" << std::fixed << std::setprecision(2) << price << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. #include <iomanip> भूल जाना, ताकि setw और setprecision undefined हों।
  2. यह उम्मीद करना कि setw persist करेगा, जब यह सिर्फ अगले output item पर apply होता है।
  3. fixed के बिना setprecision(2) इस्तेमाल करना और दो decimal places की बजाय significant digits पाना।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.