/* bufdemo.c * lecture slide 11 from CMU systems course at * https://www.cs.cmu.edu/afs/cs/academic/class/ * 15213-f15/www/lectures/09-machine-advanced.pdf */ #include /* Get string from stdin ... an implementation of gets(). */ char* my_gets(char* dest) { char* p = dest; // p is pointer to start of destination. int c = getchar(); // Get a char from stdin. while (c != EOF && c != '\n') { // Continue unless EndOfFile or newline. *p++ = c; // Put char in buf then increment pointer. c = getchar(); // Get next character. } *p = '\0'; // End of string; add null terminator. return dest; } void echo(){ // Quick quiz: how many characters can this buffer hold? char buf[4]; // Way too small! (BTW, how big is big enough?) my_gets(buf); // Read a string into the buffer. puts(buf); // Print the string in the buffer. } void call_echo(){ echo(); } int main(){ printf("Type a string: "); call_echo(); return 0; }