C++ में cin और cout
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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?
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- गलत arrow direction इस्तेमाल करना, जैसे
cin << x;याcout >> x;; input>>इस्तेमाल करता है और output<<। cin >> nameसे पढ़ना और एक पूरे sentence की उम्मीद करना, जब यह पहली whitespace पर रुक जाता है।std::याusing namespace std;भूल जाना, ताकिcinऔरcoutन मिलें।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: