Kernighans getch() a standard?

In the classic textbook The C Programming Language, they define a function getch(), for:
It is often the case that a program cannot determine that it has read enough input until it has read too much. One instance is collecting characters that make up a number: until the first non-digit is seen, the number is not complete. But then the program has read one character too far, a character that it is not prepared for.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#define BUFSIZE 100
char buf[BUFSIZE]; /* buffer for ungetch */
int bufp = 0; /* next free position in buf */
int getch(void) /* get a (possibly pushed-back) character */
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c) /* push character back on input */
{
if (bufp >= BUFSIZE)
printf("ungetch: too many characters\n");
else
buf[bufp++] = c;
}


Note that this is a custom function used in the textbook, and looking through literature, is not a standard function. Yet, if I include it in my source file and compile and exectute, it works without ever defining it. The library I included was #include<stdio.h>.
if it compiles and runs, and doesnt complain about "not defined", it can only mean getch() is in stdio.h. But literature and common knowledge says it isnt. Hence a Paradox...

How is this paradox explained?

Nonstandard extension
I just read up on "nonstandard extensions" and never knew such things existed. Interesting, thanks bazzy. Do non standard extensions vary according to compiler? Im assuming there will be on the order of hundreds of nonstandard extensions due to countless hundreds functions that are used very often being borderline "standard".
Most extensions are compiler-specific but some may be found implemented in different compilers
Even if you know that an extension is common, you should avoid using it ( since the compiler implementors may change it )
Each compiler is required to document the presence of these, you can refer to the manuals of your compiler to see what extensions it provides and how you can disable them
Topic archived. No new replies allowed.