Introduction
C++ में program को interactive बनाने के लिए input और output का उपयोग किया जाता है। जब program user से data लेता है और result को screen पर दिखाता है, तब input-output operations की आवश्यकता होती है।
C++ में standard input और output के लिए cin और cout का उपयोग किया जाता है, जो <iostream> header file के अंतर्गत आते हैं। ये दोनों stream objects हैं और data को program और user के बीच exchange करने का काम करते हैं।
Definition
cin
cin एक input object है जिसका उपयोग user से data लेने के लिए किया जाता है। यह keyboard से input प्राप्त करता है।
cout
cout एक output object है जिसका उपयोग screen (monitor) पर data को display करने के लिए किया जाता है।
Header File और Namespace
cin और cout का उपयोग करने के लिए:
#include <iostream>
using namespace std;
cout का उपयोग
cout का उपयोग output दिखाने के लिए किया जाता है और इसमें insertion operator << का उपयोग होता है।
Example (cout)
#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
Output:
Hello World
cin का उपयोग
cin का उपयोग input लेने के लिए किया जाता है और इसमें extraction operator >> का उपयोग होता है।
Example (cin)
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter age: ";
cin >> age;
cout << "Age is: " << age;
return 0;
}
Output:
Enter age: 20
Age is: 20
Multiple Inputs और Outputs
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum: " << a + b;
return 0;
}
Output:
Enter two numbers: 5 3
Sum: 8
cin और cout के Operators
<<→ Insertion Operator (cout के साथ)>>→ Extraction Operator (cin के साथ)
Important Points
cininput लेता है (keyboard से)coutoutput दिखाता है (screen पर)- दोनों
<iostream>का हिस्सा हैं using namespace std;सेstd::लिखने की जरूरत नहीं होती
निष्कर्ष
cin और cout C++ में input और output के लिए सबसे basic और महत्वपूर्ण tools हैं। इनके माध्यम से program user के साथ interaction करता है और data को process करके result प्रदान करता है।