Which of the following is the correct syntax to read the single character from…
2023
Which of the following is the correct syntax to read the single character from console in the C++ language?
- A.
read ch()
- B.
getlinech()
- C.
getch()
- D.
More than one of the above
- E.
None of the above
Attempted by 276 students.
Show answer & explanation
Correct answer: C
In C++, the correct function to read a single character from the console is get(). The syntax is get(ch), where ch is a variable of type char. This function reads the next character from the input stream, including whitespace.
Option A (read ch()) is incorrect because read() is not a standard C++ function for character input, and the syntax is invalid.
Option B (getlinech()) is incorrect because getline() is used to read a line of text, not a single character, and the syntax is invalid.
Option D (More than one of the above) is incorrect because only one option has the correct syntax.
Option E (None of the above) is incorrect because get(ch) is a valid syntax for reading a single character.
हिन्दी उत्तर: C++ में कंसोल से एकल कैरेक्टर पढ़ने के लिए सही फंक्शन get() है। सिंटैक्स get(ch) है, जहाँ ch एक char प्रकार का चर है। यह फंक्शन इनपुट स्ट्रीम से अगले कैरेक्टर को पढ़ता है, स्पेस शामिल है।
विकल्प A (read ch()) गलत है क्योंकि read() एक मानक C++ फंक्शन नहीं है और सिंटैक्स अमान्य है।
विकल्प B (getlinech()) गलत है क्योंकि getline() एक लाइन टेक्स्ट पढ़ने के लिए उपयोग किया जाता है, न कि एकल कैरेक्टर के लिए, और सिंटैक्स अमान्य है।
विकल्प D (उपर्युक्त में से एक से अधिक) गलत है क्योंकि केवल एक विकल्प सही सिंटैक्स है।
विकल्प E (उपर्युक्त में से कोई नहीं) गलत है क्योंकि get(ch) एकल कैरेक्टर पढ़ने के लिए एक मान्य सिंटैक्स है।