I don't understand what you mean by "all bufp variables". There's only one. But it all works perfectly fine.
Code:
// main.c ///////////////////////////////////////////
#include <stdio.h>
#include <ctype.h>
#include "getch.h"
// A main to test it.
int main(){
int ch;
while (isspace(ch = getch()))
;
printf("[%c]\n", ch);
ungetch(ch);
printf("[%c]\n", getch());
printf("[%c]\n", getch());
return 0;
}
// getch.h ///////////////////////////////////////////
#ifndef GETCH_H
#define GETCH_H
void ungetch(int ch);
int getch(void);
#endif
// getch.c ///////////////////////////////////////////
#include <stdio.h>
// We would usually include the header here, too (but it works either way)
#include "getch.h"
#define BUFSIZE 100
// These would normally be static (but it works either way)
static char buf[BUFSIZE];
static int bufp = 0;
void ungetch(int character){
if (bufp >= BUFSIZE)
printf("Existen muchos caracteres\n");
else
buf[bufp++] = character;
}
int getch(void){
return (bufp > 0) ? buf[--bufp] : getchar();
}
To compile and run it:
Code:
$ gcc -c main.c
$ gcc -c getch.c
$ gcc main.o getch.o
$ ./a.out
hello
[h]
[h]
[e]