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

C++ में cin और cout

cout screen पर चीज़ें दिखाता है और cin वह पढ़ता है जो व्यक्ति type करता है, बोलने और सुनने की तरह। साथ में वे आपके program को एक बातचीत करने देते हैं।
Syntax
cpp
std::cout << value << std::endl;

std::cin >> variable_name;

cout क्या है?

cout standard output stream है, text, numbers, और variable values को console पर भेजने के लिए insertion operator << के साथ इस्तेमाल होता है।

यह वह tool है जिसे आप इस्तेमाल करते हैं जब भी आपके program को इसे चलाने वाले व्यक्ति को कुछ display करना हो, एक simple greeting से लेकर एक computed result तक।

उदाहरण: What is cout?

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

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Hello, world!" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

cin क्या है?

cin standard input stream है, user जो भी type करे उसे पढ़ने और इसे एक variable में store करने के लिए extraction operator >> के साथ pair किया गया है।

यह typed text को automatically variable के type में convert करता है, इसलिए cin >> age जहाँ age एक int है user के entered digits को एक number की तरह parse करेगा।

उदाहरण: What is cin?

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("25"); // stand-in for real std::cin input
	int age;
	cin >> age;
	std::cout << "Age: " << age << std::endl;
	return 0;
}

cout से Output Chain करना

एक statement में कई << operators chain करना, जैसे cout << "Sum: " << total << endl;, आपको अलग print statements लिखने की बजाय labels और values को एक readable line में combine करने देता है।

यह cascading style idiomatic C++ है और output code को compact रखता है।

उदाहरण: Chaining Output with cout

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

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

cin के साथ Multiple Inputs

आप same तरीके से >> chain करके एक line में कई values पढ़ सकते हैं, जैसे cin >> width >> height;, और C++ automatically spaces, tabs, और newlines को values के बीच separators की तरह treat करता है।

इसका मतलब है user 5 10 type कर सकता है या numbers के बीच Enter दबा सकता है और दोनों तरह से identically काम करता है।

उदाहरण: Multiple Inputs with cin

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("5 10"); // stand-in for "cin >> width >> height;"
	int width, height;
	cin >> width >> height;
	std::cout << width << " " << height << std::endl;
	return 0;
}

endl और Newline के साथ cout

endl और \n दोनों output को एक नई line पर ले जाते हैं, लेकिन endl additionally output buffer flush करता है, अब तक print हुए text को actually तुरंत लिखे जाने की guarantee देते हुए।

\n tight loops में तेज़ है क्योंकि यह वह flush skip करता है, इसलिए कई C++ programmers routine output के लिए \n पसंद करते हैं और endl को उन moments के लिए रखते हैं जहाँ एक immediate flush genuinely मायने रखता है, जैसे एक crash या एक long-running operation से ठीक पहले।

उदाहरण: cout with endl and Newline

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

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << "Flushed immediately" << std::endl;
	// Print to the console with cout
	std::cout << "Faster in tight loops" << "\n";
	// 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. गलत arrow direction इस्तेमाल करना, जैसे cin << x; या cout >> x;; input >> इस्तेमाल करता है और output <<।
  2. cin >> name से पढ़ना और एक पूरे sentence की उम्मीद करना, जब यह पहली whitespace पर रुक जाता है।
  3. std:: या using namespace std; भूल जाना, ताकि cin और cout न मिलें।
🔒

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.