From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/io%2Fgetchar.html | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 devdocs/c/io%2Fgetchar.html (limited to 'devdocs/c/io%2Fgetchar.html') diff --git a/devdocs/c/io%2Fgetchar.html b/devdocs/c/io%2Fgetchar.html new file mode 100644 index 00000000..51e96aa8 --- /dev/null +++ b/devdocs/c/io%2Fgetchar.html @@ -0,0 +1,49 @@ +

getchar

Defined in header <stdio.h>
int getchar( void );
+

Reads the next character from stdin.

+

Equivalent to getc(stdin).

+

Parameters

(none)

+

Return value

The obtained character on success or EOF on failure.

+

If the failure has been caused by end-of-file condition, additionally sets the eof indicator (see feof()) on stdin. If the failure has been caused by some other error, sets the error indicator (see ferror()) on stdin.

+

Example

+

Demonstrates getchar with error checking

+
#include <stdio.h>
+#include <stdlib.h>
+ 
+int main(void)
+{
+    for (int ch; (ch = getchar()) != EOF;) // read/print "abcde" from stdin
+        printf("%c", ch);
+ 
+    // Test reason for reaching EOF.
+    if (feof(stdin)) // if failure caused by end-of-file condition
+        puts("End of file reached");
+    else if (ferror(stdin)) // if failure caused by some other error
+    {
+        perror("getchar()");
+        fprintf(stderr, "getchar() failed in file %s at line # %d\n",
+                __FILE__, __LINE__ - 9);
+        exit(EXIT_FAILURE);
+    }
+ 
+    return EXIT_SUCCESS;
+}

Possible output:

+
abcde
+End of file reached

References

See also

+ +
gets a character from a file stream
(function)
C++ documentation for getchar
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/io/getchar +

+
-- cgit v1.2.3