Predefined Streams in C++ (cin, cout, cerr and clog) - Explained with Examples
C++ provides four predefined standard stream objects: cin, cout, cerr, and clog. These objects are used to perform input and output operations through standard devices. They are declared in the <iostream> header file and are available in the std namespace. The C++ input/output stream library provides classes such as istream, ostream, and iostream. The objects cin, cout, cerr, and clog are associated with these stream classes.
C++ में चार Predefined Standard Stream Objects उपलब्ध होते हैं: cin, cout, cerr और clog। इन Objects का उपयोग Standard Devices के माध्यम से Input और Output Operations करने के लिए किया जाता है। ये सभी <iostream> Header File में Declared होते हैं तथा std Namespace में उपलब्ध होते हैं। C++ की Input/Output Stream Library में istream, ostream और iostream जैसी Classes उपलब्ध होती हैं। cin, cout, cerr और clog Objects इन Stream Classes से संबंधित होते हैं।
Types of Predefined Streams in C++ | C++ में Predefined Streams के प्रकार
C++ provides the following four standard stream objects:
C++ में निम्नलिखित चार Standard Stream Objects उपलब्ध होते हैं:
cin– Standard Input Streamcout– Standard Output Streamcerr– Standard Error Streamclog– Standard Logging Stream
1. cin – Standard Input Stream
The cin object is an object of the istream class. It is used to accept input from the standard input device, which is usually the keyboard. It uses the extraction operator (>>) to read input from the standard input stream (stdin).
cin Object, istream Class का एक Object है। इसका उपयोग Standard Input Device, जो सामान्यतः Keyboard होता है, से Input प्राप्त करने के लिए किया जाता है। यह Standard Input Stream (stdin) से Input Read करने के लिए Extraction Operator (>>) का प्रयोग करता है।
Syntax:
cin >> variable_name;Example:
int age;cout << "Enter your age: ";cin >> age;
Output:
Enter your age: 25Explanation | व्याख्या:
In this example, cin accepts an integer value from the keyboard and stores it in the variable age.
इस उदाहरण में cin Keyboard से एक Integer Value प्राप्त करता है और उसे Variable age में Store करता है।
2. cout – Standard Output Stream
The cout object is an object of the ostream class. It is used to display output on the standard output device, which is usually the monitor. It uses the insertion operator (<<) to send output to the standard output stream (stdout).
cout Object, ostream Class का एक Object है। इसका उपयोग Standard Output Device, जो सामान्यतः Monitor होता है, पर Output Display करने के लिए किया जाता है। यह Standard Output Stream (stdout) पर Output भेजने के लिए Insertion Operator (<<) का प्रयोग करता है।
Syntax:
cout << "Message";Example:
float p, r, t;cout << "Enter the value of principal, rate and time" << endl;
Output:
Enter the value of principal, rate and timeExplanation | व्याख्या:
In this example, cout displays a message on the monitor. The endl manipulator moves the cursor to the next line and flushes the output stream.
इस उदाहरण में cout Monitor पर एक Message Display करता है। endl Manipulator Cursor को अगली Line पर ले जाता है और Output Stream को Flush भी करता है।
3. cerr – Standard Error Stream
The cerr object is an object of the ostream class. It is used to display error messages on the standard error device, which is usually the monitor. It is commonly used to report errors during program execution. The cerr stream is unbuffered, so error messages are sent to the associated output device without waiting for the usual buffer to fill.
cerr Object, ostream Class का एक Object है। इसका उपयोग Standard Error Device, जो सामान्यतः Monitor होता है, पर Error Messages Display करने के लिए किया जाता है। इसका उपयोग Program Execution के दौरान उत्पन्न Errors की जानकारी देने के लिए किया जाता है। cerr Stream Unbuffered होता है, इसलिए Error Messages सामान्य Buffer के भरने की प्रतीक्षा किए बिना संबंधित Output Device पर भेजे जाते हैं।
Syntax:
cerr << "Error Message";Example:
#include <iostream>using namespace std;int main(){int age = -5;if (age < 0){cerr << "Error: Age cannot be negative.";}return 0;}
Output:
Error: Age cannot be negative.Explanation | व्याख्या:
In this example, cerr is used to display an error message because the age value is negative. It is useful for reporting errors separately from normal program output.
इस उदाहरण में Age की Value Negative होने के कारण Error Message Display करने के लिए cerr का उपयोग किया गया है। यह सामान्य Program Output से अलग Errors की जानकारी देने में उपयोगी होता है।
4. clog – Standard Logging Stream
The clog object is also an object of the ostream class. It is used to display logging messages and diagnostic information. Unlike cerr, the clog stream is buffered, which means its output may be stored temporarily in a buffer before being sent to the output device.
clog Object भी ostream Class का एक Object है। इसका उपयोग Logging Messages और Diagnostic Information Display करने के लिए किया जाता है। cerr के विपरीत, clog Stream Buffered होता है, जिसका अर्थ है कि इसका Output Device पर भेजे जाने से पहले अस्थायी रूप से Buffer में Store हो सकता है।
Syntax:
clog << "Log Message";Example:
#include <iostream>using namespace std;int main(){clog << "Program execution started.";return 0;}
Output:
Program execution started.Explanation | व्याख्या:
In this example, clog displays a logging message indicating that program execution has started. It is useful for recording program activities and diagnostic information.
इस उदाहरण में clog एक Logging Message Display करता है, जो बताता है कि Program Execution शुरू हो गया है। इसका उपयोग Program की Activities और Diagnostic Information Record करने के लिए किया जाता है।
Difference Between cin, cout, cerr and clog | cin, cout, cerr और clog में अंतर
| Stream Object | Class | Purpose | Operator |
|---|---|---|---|
cin | istream | Accepts input | >> |
cout | ostream | Displays normal output | << |
cerr | ostream | Displays error messages | << |
clog | ostream | Displays logging messages | << |
Difference Between cerr and clog | cerr और clog में अंतर
Both cerr and clog are used for error reporting and diagnostic messages, but they differ in buffering behavior.
cerr और clog दोनों का उपयोग Error Reporting और Diagnostic Messages के लिए किया जाता है, लेकिन इनके Buffering Behavior में अंतर होता है।
| cerr | clog |
|---|---|
| Used for displaying error messages. | Used for displaying logging and diagnostic messages. |
| It is unbuffered. | It is buffered. |
| Output is sent without waiting for the usual buffer to fill. | Output may be temporarily stored in a buffer. |
| Useful for immediate error reporting. | Useful for general program logging. |
Program: Simple Interest Using cin and cout | cin और cout का उपयोग करके साधारण ब्याज का प्रोग्राम
The following program demonstrates the use of cin and cout to accept the principal amount, rate, and time from the user and calculate simple interest.
निम्नलिखित Program में User से Principal Amount, Rate और Time प्राप्त करने तथा Simple Interest Calculate करने के लिए cin और cout का उपयोग किया गया है।
Formula: [SI = P*R*T/100 ]
Where जहाँ:
P = मूलधन (Principal Amount), R = ब्याज दर (Rate of Interest), T = समय (Time)
Program:
#include <iostream>using namespace std;int main(){float p, r, t, si;cout << "Enter Principal, Rate and Time: ";cin >> p >> r >> t;si = (p * r * t) / 100;cout << "Simple Interest = " << si << endl;return 0;}
Output:
Enter Principal, Rate and Time: 10000 5 2Simple Interest = 1000
Explanation | व्याख्या:
In this program, cin accepts the principal amount, rate of interest, and time from the keyboard. The simple interest is calculated using the formula (p * r * t) / 100. Finally, cout displays the calculated simple interest on the monitor.
इस Program में cin Keyboard से Principal Amount, Rate of Interest और Time प्राप्त करता है। Simple Interest की गणना (p * r * t) / 100 Formula का उपयोग करके की जाती है। अंत में cout Calculate किए गए Simple Interest को Monitor पर Display करता है।
The four predefined stream objects cin, cout, cerr, and clog are important components of C++ input/output operations. cin is used for input, cout for normal output, cerr for immediate error reporting, and clog for buffered logging messages. These objects make input, output, and diagnostic operations easier in C++ programs.
C++ में चार Predefined Stream Objects cin, cout, cerr और clog Input/Output Operations के महत्वपूर्ण Components हैं। cin का उपयोग Input के लिए, cout का उपयोग सामान्य Output के लिए, cerr का उपयोग तुरंत Error Reporting के लिए तथा clog का उपयोग Buffered Logging Messages के लिए किया जाता है। ये Objects C++ Programs में Input, Output और Diagnostic Operations को आसान बनाते हैं।
Comments
Post a Comment