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

C++ के Format Specifiers

C++ आपको control करने देता है कि numbers कैसे दिखें, जैसे decimals, hexadecimal, या scientific notation दिखाना, एक measurement लिखने का तरीका चुनने जैसा। Stream tools formatting करते हैं।
Syntax
cpp
#include <iomanip>

std::cout << std::fixed << std::setprecision(digits) << decimal_value;

std::cout << std::hex << integer_value;  // or std::oct, std::dec

Floating-Point Formatting

fixed stream manipulator बहुत बड़ी या छोटी values के लिए scientific notation पर switch होने की बजाय floating-point numbers को standard decimal notation में print होने पर force करता है।

Exactly कितने digits decimal point के बाद दिखने हैं यह control करने के लिए इसे setprecision के साथ combine करें, जो money जैसी किसी भी चीज़ display करने के लिए essential है।

उदाहरण: Floating-Point Formatting

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.5
	double price = 19.5;
	// 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;
}

Hexadecimal और Octal Formats

hex और oct manipulators बदलते हैं कि बाद के integers कैसे print होते हैं — hex उन्हें base-16 में दिखाता है और oct base-8 में — जबकि dec वापस normal base-10 पर switch करता है जिसकी आप उम्मीद करते हैं।

ये low-level code debug करते समय खासकर useful हैं, जहाँ एक value का hexadecimal form देखना bit patterns और memory addresses पढ़ना कहीं आसान बना देता है।

उदाहरण: Hexadecimal and Octal Formats

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

// Program execution starts in main()
int main() {
	// Declare value and set it to 255
	int value = 255;
	// Print to the console with cout
	std::cout << std::hex << value << std::endl;
	// Print to the console with cout
	std::cout << std::oct << value << std::endl;
	std::cout << std::dec << value << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Boolean Output Format

Default से C++ boolean values को raw integers 1 और 0 की तरह print करता है, जो एक human reader के लिए intended output में confusing हो सकता है।

boolalpha apply करना cout को इसकी जगह actual words true और false print कराता है, और noboolalpha बाद में ज़रूरत पड़ने पर numeric form पर वापस switch करता है।

उदाहरण: Boolean Output Format

cpp
#include <iostream>

int main() {
	bool active = true;
	std::cout << active << std::endl; // 1
	std::cout << std::boolalpha << active << std::endl; // true
	return 0;
}

Scientific Notation

scientific floating-point numbers को exponential notation इस्तेमाल करके format करता है, जैसे 150000000 की बजाय 1.5e+08, जो बहुत बड़ी या बहुत छोटी numbers को compact और एक नज़र में compare करना आसान रखता है।

यह scientific या engineering code में मायने रखता है जहाँ values same output में कई orders of magnitude फैल सकती हैं।

उदाहरण: Scientific Notation

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

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

Base Prefix Formatting

showbase एक visible prefix add करता है जो एक number का base identify करता है — hexadecimal के लिए 0x और octal के लिए एक leading 0 — ताकि एक reader को बाद में आने वाले digits interpret करने के लिए guess न करना पड़े।

यह hex या oct के साथ सबसे useful है, क्योंकि इसके बिना ff जैसी एक hex value आसानी से decimal समझी जा सकती है।

उदाहरण: Base Prefix Formatting

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

// Program execution starts in main()
int main() {
	// Declare value and set it to 255
	int value = 255;
	// Print to the console with cout
	std::cout << std::showbase << std::hex << value << 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. cout में %d जैसे printf style specifiers ढूंढना, जब C++ streams इसकी बजाय hex और fixed जैसे manipulators इस्तेमाल करती हैं।
  2. fixed और setprecision set करना और उनके reset होने की उम्मीद करना, जब वे बाद के output के लिए effect में रहते हैं।
  3. एक bool print करना और true की उम्मीद करना, जब आपको 1 या 0 की बजाय text दिखाने के लिए boolalpha चाहिए।
🔒

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.