PGDCA, BCA, B-Tech, MCA, M-Tech, B.Sc computer science राजस्थान सूचना सहायक व कंप्यूटर शिक्षक विशेष नोट्स: इस अध्याय में हम C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std (C++ बेसिक्स: cin, cout, स्ट्रीम ऑपरेटर्स और namespace std) की संपूर्ण थ्योरी, मुख्य तकनीकी बिंदु, तुलनात्मक तालिका और विगत परीक्षाओं के पैटर्न पर आधारित 20 अति-महत्वपूर्ण बहुविकल्पीय प्रश्न (MCQs) विस्तृत हिंदी व अंग्रेजी व्याख्या सहित पढ़ेंगे।
Overview: Standard C++ console input and output use the <iostream> objects cin and cout paired with the extraction (>>) and insertion (<<) operators. All standard library identifiers reside inside namespace std, accessed via the scope resolution operator (std::) or a using declaration to prevent global name collisions.
अध्याय सार: मानक C++ कंसोल इनपुट और आउटपुट के लिए <iostream> हेडर के cin और cout ऑब्जेक्ट्स के साथ एक्सट्रैक्शन (>>) और इंसर्शन (<<) ऑपरेटर्स का उपयोग होता है। ग्लोबल नेम टकराव से बचने के लिए सभी मानक लाइब्रेरी पहचानकर्ता namespace std में रखे जाते हैं, जिन्हें std:: या using द्वारा एक्सेस किया जाता है।
C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std: Detailed Notes & Architecture (C++ बेसिक्स: cin, cout, स्ट्रीम ऑपरेटर्स और namespace std)
1. Standard Stream Objects in <iostream> (<iostream> में मानक स्ट्रीम ऑब्जेक्ट्स)
[EN] Modern standard C++ includes <iostream> (NOT the obsolete pre-standard <iostream.h>) to provide four predefined global stream objects: cout, cin, cerr, and clog. cout is an instance of ostream connected to standard output (stdout) with output buffering, while cin is an instance of istream connected to standard input (stdin). cerr (unbuffered) and clog (buffered) are ostream objects bound to standard error (stderr) so error messages appear immediately even if stdout is redirected to a file. Unlike printf()/scanf() which require manual format specifiers like %d or %lf, C++ streams are type-safe because operators deduce types automatically at compile time.
[HI] आधुनिक मानक C++ में (पुराने <iostream.h> के बजाय) <iostream> हेडर शामिल किया जाता है, जो cout, cin, cerr और clog नामक चार स्ट्रीम ऑब्जेक्ट्स प्रदान करता है। cout मानक आउटपुट (stdout) से जुड़ा बफर्ड ostream ऑब्जेक्ट है, जबकि cin मानक इनपुट (stdin) से जुड़ा istream ऑब्जेक्ट है। cerr (अनबफर्ड) और clog (बफर्ड) मानक त्रुटि स्ट्रीम (stderr) से जुड़े होते हैं ताकि मुख्य आउटपुट फाइल में जाने पर भी एरर तुरंत दिखें। printf()/scanf() के विपरीत, C++ स्ट्रीम्स टाइप-सेफ होती हैं क्योंकि ऑपरेटर्स कंपाइल समय पर डेटा टाइप स्वयं पहचान लेते हैं।
2. Insertion Operator (<<) and Output Chaining (इंसर्शन ऑपरेटर (<<) और आउटपुट चेनिंग)
[EN] The insertion operator (<<) is the overloaded bitwise left-shift operator used with cout to send data bytes into the output stream, as in cout << "Age: " << age << '\n';. Chaining multiple << operators works seamlessly because operator<< has left-to-right associativity and returns a reference (ostream&) to the same stream object (cout). For line breaks, std::endl inserts a newline character AND invokes flush() to force buffered output onto the screen immediately, whereas '\n' inserts only the newline without flushing. In competitive programming or high-throughput loops, preferring '\n' over std::endl avoids severe I/O slowdowns caused by thousands of forced buffer flushes.
[HI] इंसर्शन ऑपरेटर (<<) वास्तव में ओवरलोड किया गया बिटवाइज लेफ्ट-शिफ्ट ऑपरेटर है जो डेटा को cout स्ट्रीम में भेजता है, जैसे cout << "Age: " << age << '\n';। कई << ऑपरेटर्स को एक साथ जोड़ना (चेनिंग) संभव होता है क्योंकि यह बाएं-से-दाएं जुड़ता है और प्रत्येक कॉल वही स्ट्रीम रेफरेंस (ostream&) लौटाता है। लाइन बदलने के लिए std::endl न्यूलाइन डालने के साथ-साथ बफर को तुरंत फ्लश भी करता है, जबकि '\n' बिना फ्लश किए केवल न्यूलाइन डालता है। लूप या बड़े आउटपुट में बार-बार std::endl के बजाय '\n' का उपयोग करने से अनावश्यक फ्लश ओवरहेड बचता है और गति बढ़ती है।
3. Extraction Operator (>>) and Whitespace Handling (एक्सट्रैक्शन ऑपरेटर (>>) और व्हाइटस्पेस व्यवहार)
[EN] The extraction operator (>>) overloads the bitwise right-shift operator on cin to parse typed values from keyboard input into variables, e.g., cin >> x >> y;. Like <<, operator>> returns istream& so multiple inputs can be chained in one statement, and by default it automatically skips leading whitespace characters (spaces, tabs, and newlines). A common pitfall is reading full-name strings with cin >> name;, which stops at the first space; to read an entire line with spaces, use std::getline(std::cin, name) from <string>. When switching from cin >> num to getline(), a leftover '\n' remains in the input buffer and must be cleared using cin.ignore() or std::ws before calling getline().
[HI] एक्सट्रैक्शन ऑपरेटर (>>) कीबोर्ड से टाइप किए गए इनपुट को वेरिएबल्स में निकालने के लिए ओवरलोड किया गया राइट-शिफ्ट ऑपरेटर है, जैसे cin >> x >> y;। यह istream& लौटाकर चेनिंग की अनुमति देता है और डिफ़ॉल्ट रूप से शुरुआती व्हाइटस्पेस (स्पेस, टैब, न्यूलाइन) को स्वचालित रूप से छोड़ देता है। cin >> name का उपयोग करने पर यह पहले स्पेस पर रुक जाता है; स्पेस सहित पूरी लाइन पढ़ने के लिए <string> के std::getline(std::cin, name) का प्रयोग करना चाहिए। cin >> num के बाद getline() चलाने से पहले बफर में बचा '\n' हटाने के लिए cin.ignore() का उपयोग जरूरी होता है।
4. Namespace std and Scope Resolution (::) (Namespace std और स्कोप रेज़ॉल्यूशन (::))
[EN] A namespace groups related symbols inside a named scope so functions or variables with identical names from different libraries do not collide during compilation or linking. The C++ Standard Library places cin, cout, endl, string, and vector inside namespace std, accessed explicitly with the scope resolution operator (::) as std::cout or std::cin. Writing using namespace std; (a using-directive) brings every symbol from std into the global scope, which saves typing in short scripts but can trigger subtle ambiguity errors with names like count, max, or distance in large projects. Professional C++ code either keeps explicit std:: prefixes or uses targeted using-declarations (using std::cout;) inside .cpp source files, and never puts using namespace std; inside header (.h/.hpp) files.
[HI] नेमस्पेस संबंधित नामों को एक नामित दायरे में समूहित करता है ताकि विभिन्न लाइब्रेरीज़ के समान नाम वाले फंक्शन या वेरिएबल आपस में न टकराएं। C++ मानक लाइब्रेरी cin, cout, endl, string और vector को namespace std के भीतर रखती है, जिन्हें std::cout या std::cin लिखकर :: (स्कोप रेज़ॉल्यूशन) से एक्सेस किया जाता है। using namespace std; लिखने से std के सभी नाम सीधे उपलब्ध हो जाते हैं, जो छोटे प्रोग्राम में सरल लगता है पर बड़े कोड में count या distance जैसे नामों से टकराव पैदा कर सकता है। व्यावसायिक कोड में या तो सीधे std:: लिखा जाता है या .cpp में सीमित using std::cout; प्रयुक्त होता है, लेकिन हेडर फाइल्स में using namespace std; कभी नहीं लिखा जाता।
Syntax, Formula & Technical Example (C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std)
#include <iostream>
#include <string>
int main() {
int roll;
std::string fullName;
std::cout << "Enter roll & name: ";
std::cin >> roll;
std::getline(std::cin >> std::ws, fullName);
std::cout << roll << " -> " << fullName << std::endl;
return 0;
}
C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std Quick Reference & Comparison Table
| Element | Technical Meaning | Key Exam Fact |
|---|---|---|
| cout << expr | Insertion operator on ostream | Arrows point data out toward cout |
| cin >> var | Extraction operator on istream | Stops at whitespace for strings |
| std::endl | Inserts '\n' and flushes output buffer | Slower than '\n' inside large loops |
| namespace std | Scope holding standard C++ identifiers | Avoid using namespace std in headers |
C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std Top 20 Bilingual MCQs with Answers
Q1. Which operator is known as the insertion operator when used with cout in C++?
(C++ में cout के साथ उपयोग होने पर किस ऑपरेटर को इंसर्शन (Insertion) ऑपरेटर कहा जाता है?)
(A) ::
(B) ->
(C) >>
(D) <<
Correct Answer: (D)
[EN] The left-shift operator << is overloaded as the stream insertion operator for output objects like cout.
[HI] लेफ्ट-शिफ्ट ऑपरेटर << को cout जैसे आउटपुट ऑब्जेक्ट्स के लिए स्ट्रीम इंसर्शन ऑपरेटर के रूप में ओवरलोड किया गया है।
Q2. Which operator is known as the extraction operator when used with cin in C++?
(C++ में cin के साथ उपयोग होने पर किस ऑपरेटर को एक्सट्रैक्शन (Extraction) ऑपरेटर कहा जाता है?)
(A) >>
(B) .
(C) &
(D) <<
Correct Answer: (A)
[EN] The right-shift operator >> is overloaded as the stream extraction operator to pull input from cin into variables.
[HI] राइट-शिफ्ट ऑपरेटर >> को cin से वेरिएबल्स में डेटा निकालने वाले एक्सट्रैक्शन ऑपरेटर के रूप में ओवरलोड किया गया है।
Q3. Which header file declares std::cin, std::cout, and std::cerr in standard ISO C++?
(मानक ISO C++ में std::cin, std::cout और std::cerr किस हेडर फाइल में घोषित होते हैं?)
(A) <iostream>
(B) <iostream.h>
(C) <conio.h>
(D) <stdio.h>
Correct Answer: (A)
[EN] Standard C++ uses <iostream> without the .h extension; <iostream.h> was an obsolete pre-standard header.
[HI] मानक C++ में बिना .h के <iostream> का उपयोग होता है; <iostream.h> एक पुराना अप्रचलित हेडर था।
Q4. What does std::endl do in addition to printing a newline character?
(एक न्यूलाइन कैरेक्टर प्रिंट करने के अलावा std::endl अतिरिक्त क्या कार्य करता है?)
(A) Adds a tab space after the newline
(B) Flushes the output buffer immediately
(C) Clears the input buffer of cin
(D) Terminates the main() function
Correct Answer: (B)
[EN] std::endl writes '\n' and calls flush() on the stream, which forces buffered data to the console immediately.
[HI] std::endl '\n' लिखने के साथ स्ट्रीम पर flush() भी कॉल करता है, जिससे बफर्ड आउटपुट तुरंत स्क्रीन पर आ जाता है।
Q5. If the user types "Ramesh Kumar" and executes cin >> name; (where string name;), what gets stored in name?
(यदि उपयोगकर्ता "Ramesh Kumar" टाइप करता है और cin >> name; चलाता है, तो name में क्या स्टोर होगा?)
(A) "Ramesh Kumar"
(B) "Kumar"
(C) An empty string
(D) "Ramesh"
Correct Answer: (D)
[EN] The extraction operator >> reads whitespace-delimited tokens, so it stops at the space after "Ramesh".
[HI] एक्सट्रैक्शन ऑपरेटर >> व्हाइटस्पेस पर रुक जाता है, इसलिए वह "Ramesh" के बाद स्पेस आते ही पढ़ना बंद कर देता है।
Q6. Why is chaining like cout << a << b; valid syntax in C++?
(C++ में cout << a << b; जैसी चेनिंग सिंटैक्स वैध क्यों होती है?)
(A) a and b must have identical data types
(B) operator<< returns an ostream& reference to the stream itself
(C) cout is an array of pointers
(D) The compiler rewrites it into two separate printf() calls
Correct Answer: (B)
[EN] Because operator<< returns a reference to cout (ostream&), the result of (cout << a) becomes the left operand for << b.
[HI] चूंकि operator<< स्वयं cout का रेफरेंस (ostream&) लौटाता है, इसलिए (cout << a) का परिणाम अगले << b के लिए बायां ऑपरेंड बन जाता है।
Q7. Which predefined stream object in <iostream> is unbuffered and intended for immediate error reporting?
(<iostream> में कौन-सा पूर्व-परिभाषित स्ट्रीम ऑब्जेक्ट अनबफर्ड होता है और तुरंत त्रुटि दिखाने के लिए उपयोग होता है?)
(A) cerr
(B) cout
(C) clog
(D) cin
Correct Answer: (A)
[EN] std::cerr is bound to standard error and is unbuffered by default, whereas std::clog is buffered.
[HI] std::cerr मानक एरर स्ट्रीम से जुड़ा होता है और डिफ़ॉल्ट रूप से अनबफर्ड होता है, जबकि std::clog बफर्ड होता है।
Q8. What is the primary purpose of the namespace keyword in C++?
(C++ में namespace कीवर्ड का मुख्य उद्देश्य क्या है?)
(A) To allocate dynamic heap memory faster
(B) To convert C++ code into Python
(C) To inherit private members of a class
(D) To group identifiers under a named scope and prevent name collisions
Correct Answer: (D)
[EN] Namespaces partition global scope into logical regions so identical symbol names from separate libraries do not clash.
[HI] नेमस्पेस ग्लोबल स्कोप को नामित भागों में बांटता है ताकि अलग-अलग लाइब्रेरीज़ के समान नाम वाले प्रतीकों में टकराव न हो।
Q9. What symbol is the scope resolution operator used to access std::cout without a using directive?
(बिना using डायरेक्टिव के std::cout को एक्सेस करने के लिए प्रयुक्त स्कोप रेज़ॉल्यूशन ऑपरेटर कौन-सा चिन्ह है?)
(A) .
(B) :
(C) ::
(D) ->
Correct Answer: (C)
[EN] The double-colon operator :: qualifies an identifier with its enclosing namespace or class scope.
[HI] डबल-कोलन ऑपरेटर (::) किसी पहचानकर्ता को उसके नेमस्पेस या क्लास स्कोप के साथ जोड़कर एक्सेस करता है।
Q10. Which function reads an entire line including spaces into a std::string variable?
(स्पेस सहित पूरी लाइन को std::string वेरिएबल में पढ़ने के लिए कौन-सा फंक्शन प्रयुक्त होता है?)
(A) std::cout << str
(B) std::cin.get(str)
(C) std::getline(std::cin, str)
(D) std::cin >> str
Correct Answer: (C)
[EN] std::getline(istream&, string&) reads characters until '\n', preserving embedded spaces.
[HI] std::getline(istream&, string&) '\n' आने तक सभी कैरेक्टर्स पढ़ता है और बीच के स्पेस को सुरक्षित रखता है।
Q11. What is the official base class type of the standard output object cout?
(मानक आउटपुट ऑब्जेक्ट cout किस क्लास टाइप का ऑब्जेक्ट (इंस्टेंस) है?)
(A) ostream
(B) ifstream
(C) istream
(D) stdio
Correct Answer: (A)
[EN] cout, cerr, and clog are objects of std::ostream (derived from basic_ostream<char>), while cin is an std::istream.
[HI] cout, cerr और clog std::ostream क्लास के ऑब्जेक्ट्स हैं, जबकि cin std::istream क्लास का ऑब्जेक्ट है।
Q12. What happens if a user enters letters like "abc" when the program executes int x; cin >> x;?
(जब प्रोग्राम int x; cin >> x; चलाता है और उपयोगकर्ता "abc" टाइप कर दे, तो क्या होता है?)
(A) cin enters a fail state (failbit set), x stays unchanged (or 0 since C++11), and subsequent reads fail until cleared
(B) cin converts 'a' to ASCII 97 and stores 97 in x
(C) cin skips "abc" automatically without failing
(D) The OS immediately kills the process
Correct Answer: (A)
[EN] Invalid format extraction sets cin's failbit, leaves stream extraction blocked until cin.clear() + cin.ignore() reset it.
[HI] अमान्य इनपुट मिलने पर cin का failbit सेट हो जाता है और cin.clear() व cin.ignore() चलाने तक आगे की रीडिंग रुक जाती है।
Q13. Why is placing using namespace std; inside a C++ header (.h / .hpp) file considered bad practice?
(किसी C++ हेडर (.h / .hpp) फाइल के अंदर using namespace std; लिखना खराब अभ्यास क्यों माना जाता है?)
(A) It doubles the executable file size
(B) Header files cannot compile with namespaces
(C) It disables cin and cout in main()
(D) It forces global std pollution into every source file that #includes that header, causing unpredictable name clashes
Correct Answer: (D)
[EN] Every .cpp file including that header inherits all std symbols silently, potentially conflicting with user-defined names like count or move.
[HI] उस हेडर को #include करने वाली हर .cpp फाइल में std के सभी नाम जबरदस्ती आ जाते हैं, जिससे नाम टकराव की संभावना बढ़ जाती है।
Q14. What happens when you write cin >> num; immediately followed by std::getline(cin, line); without clearing the buffer?
(यदि बिना बफर साफ किए cin >> num; के तुरंत बाद std::getline(cin, line); चलाया जाए, तो क्या होता है?)
(A) getline() deletes the value stored in num
(B) getline() consumes the leftover '\n' from num's input and returns an empty string
(C) The compiler refuses to link <string>
(D) cin waits twice for num
Correct Answer: (B)
[EN] cin >> num stops right before the Enter key's '\n', so getline() sees that immediate '\n' as an empty line unless cin.ignore() or std::ws consumes it.
[HI] cin >> num एंटर के '\n' को बफर में छोड़ देता है, जिसे getline() तुरंत खाली लाइन मानकर पढ़ लेता है।
Q15. What is the associativity of the stream operators << and >> in C++ expressions?
(C++ एक्सप्रेशन्स में स्ट्रीम ऑपरेटर्स << और >> की एसोसिएटिविटी (Associativity) क्या होती है?)
(A) Compiler-dependent
(B) Right-to-left
(C) Non-associative
(D) Left-to-right
Correct Answer: (D)
[EN] Both << and >> associate left-to-right, evaluating (cout << a) before << b.
[HI] << और >> दोनों की एसोसिएटिविटी बाएं-से-दाएं (Left-to-right) होती है।
Q16. Which of the following statements about cin, cout, and namespaces in C++ is NOT true?
(C++ में cin, cout और नेमस्पेस के बारे में निम्नलिखित में से कौन-सा कथन सत्य नहीं है?)
(A) using std::cout; imports only cout instead of the entire std namespace
(B) cin and cout require %d and %s format specifiers just like scanf() and printf()
(C) std::endl flushes the stream buffer
(D) cout is buffered whereas cerr is unbuffered by default
Correct Answer: (B)
[EN] C++ stream operators (<< and >>) are overloaded per type and do NOT use printf/scanf format specifiers.
[HI] C++ स्ट्रीम ऑपरेटर्स (<< और >>) टाइप-ओवरलोडेड होते हैं और इन्हें %d या %s जैसे फॉर्मेट स्पेसिफायर्स की आवश्यकता नहीं होती।
Q17. Within C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std, which element directly maps to: "Insertion operator on ostream"?
(C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std में कौन सा Element सीधे "Insertion operator on ostream" से संबंधित है?)
(A) cout << expr
(B) namespace std
(C) std::endl
(D) cin >> var
Correct Answer: (A)
[EN] In C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std, 'cout << expr' handles 'Insertion operator on ostream' (Arrows point data out toward cout).
[HI] C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std में 'cout << expr' का कार्य/विवरण 'Insertion operator on ostream' (Arrows point data out toward cout) है।
Q18. Which option accurately describes 'cin >> var' in C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std?
(C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std के संदर्भ में 'cin >> var' की सही विशेषता कौन सी है?)
(A) Scope holding standard C++ identifiers
(B) Extraction operator on istream
(C) Inserts '\n' and flushes output buffer
(D) Insertion operator on ostream
Correct Answer: (B)
[EN] 'cin >> var' provides 'Extraction operator on istream' — key point: Stops at whitespace for strings.
[HI] 'cin >> var' का मुख्य विवरण 'Extraction operator on istream' है (Stops at whitespace for strings)।
Q19. In C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std, identify the correct parameter for the specification: "Inserts '\n' and flushes output buffer".
(C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std में "Inserts '\n' and flushes output buffer" विवरण किस विकल्प के लिए सही है?)
(A) std::endl
(B) cout << expr
(C) namespace std
(D) cin >> var
Correct Answer: (A)
[EN] 'std::endl' corresponds to 'Inserts '\n' and flushes output buffer' (Slower than '\n' inside large loops).
[HI] 'std::endl' का संबंध 'Inserts '\n' and flushes output buffer' (Slower than '\n' inside large loops) से है।
Q20. Considering C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std, what key behavior defines 'namespace std'?
(C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std में 'namespace std' की मुख्य भूमिका या मान क्या है?)
(A) Insertion operator on ostream
(B) Extraction operator on istream
(C) Inserts '\n' and flushes output buffer
(D) Scope holding standard C++ identifiers
Correct Answer: (D)
[EN] 'namespace std' represents 'Scope holding standard C++ identifiers' (Avoid using namespace std in headers).
[HI] 'namespace std' का अर्थ/मान 'Scope holding standard C++ identifiers' है (Avoid using namespace std in headers)।
C++ Basics: cin, cout, Extraction/Insertion (>>, <<), namespace std — Exam FAQs
Q1: When should I use '\n' instead of std::endl?
मुझे std::endl के बजाय '\n' का उपयोग कब करना चाहिए?
[EN] Use '\n' for normal line breaks (especially inside loops) for faster buffered output, and reserve std::endl only when you explicitly need the console flushed right away.
[HI] सामान्य लाइन बदलने (विशेषकर लूप के अंदर) के लिए '\n' अधिक तेज होता है; std::endl का उपयोग केवल तभी करें जब तुरंत बफर फ्लश करना अनिवार्य हो।
Q2: What is the difference between using namespace std; and using std::cout;?
using namespace std; और using std::cout; के बीच क्या अंतर है?
[EN] using namespace std; exposes every identifier in std (directive), whereas using std::cout; imports only that single name cleanly (declaration).
[HI] using namespace std; पूरे std नेमस्पेस के सभी नाम खोल देता है, जबकि using std::cout; केवल cout को ही आयात करता है।
Q3: Can cin and cout be synchronized with C's stdio functions like printf() and scanf()?
क्या C++ में cin और cout को C के printf() और scanf() के साथ एकसाथ उपयोग किया जा सकता है?
[EN] Yes, by default std::ios::sync_with_stdio(true) keeps C and C++ streams synchronized; setting it to false boosts I/O speed if you stop mixing C stdio with C++ streams.
[HI] हाँ, डिफ़ॉल्ट रूप से std::ios::sync_with_stdio(true) दोनों को सिंक रखता है; यदि आप C और C++ I/O को न मिलाएं तो इसे false करके गति बढ़ाई जा सकती है।
Post a Comment
Post a Comment