std::getchar(3)
π std::getchar(3) π
okay so welcome to the std::getchar(3) page from the C++ Standard Libary (that's the manual name, and yes it's spelled that way in the original β not me, bestie π
). This is the std::getchar page, and she's lowkey a whole main character.
πΈ NAME πΈ
std::getchar - std::getchar. That's it. That's the name. The algorithm is: be named exactly that. Periodt. π
β¨ Synopsis β¨
Defined in header <cstdio>:
int getchar();
Reads the next character from stdin.
Equivalent to std::getc(stdin). She's literally std::getc with stdin locked in β no cap. π«Ά
π Parameters π
(none)β not a single parameter. She takes zero arguments and still eats.
π¦ Return value π¦
- Success: the obtained character.
- Failure:
EOF.
If the failure has been caused by end of file condition, additionally sets the eof indicator (see std::feof()) on stdin. If the failure has been caused by some other error, sets the error indicator (see std::ferror()) on stdin.
π₯ Example π₯
std::getchar with error checking. Exit program by entering ESC char. Here's the whole vibe:
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <iostream>
int main()
{
for (int ch; (ch = std::getchar()) != EOF ;) // read/print "abc" from stdin
{
if (std::isprint(ch))
std::cout << static_cast<char>(ch) << '\n';
if (ch == 27) // 'ESC' (escape) in ASCII
return EXIT_SUCCESS;
}
// Test reason for reaching EOF.
if (std::feof(stdin)) // if failure caused by end-of-file condition
std::cout << "End of file reached\n";
else if (std::ferror(stdin)) // if failure caused by some other error
{
std::perror("getchar()");
std::cerr << "getchar() failed in file " << std::quoted(__FILE__)
<< " at line # " << __LINE__ - 14 << '\n';
std::exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
π Possible output π
abc
a
b
c
^[
That last line? That's the ESC char moment β iconic, honestly. π
π See also π
fgetcβ gets a character from a file streamgetcβ (function)- C documentation for
getcharβ the C docs forgetchar
okay that's the whole page! source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::getchar(3) β love u, bestie, go read some chars πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms