An Interactive Prompt


For the basic setup we want to write a loop which repeatedly writes out a message, and then waits for some input. To get user input we can use a function called fgets, which reads any input up until a new line. We need somewhere to store this user input. For this we can declare a constantly sized input buffer.

Once we have this user input stored we can then print it back to the user using a function called printf.

  1. #include <stdio.h>
  2. /* Declare a buffer for user input of size 2048 */
  3. static char input[2048];
  4. int main(int argc, char** argv) {
  5. /* Print Version and Exit Information */
  6. puts("Lispy Version 0.0.0.0.1");
  7. puts("Press Ctrl+c to Exit\n");
  8. /* In a never ending loop */
  9. while (1) {
  10. /* Output our prompt */
  11. fputs("lispy> ", stdout);
  12. /* Read a line of user input of maximum size 2048 */
  13. fgets(input, 2048, stdin);
  14. /* Echo input back to user */
  15. printf("No you're a %s", input);
  16. }
  17. return 0;
  18. }

What is that text in light green?

The above code contains comments. These are sections of the code between /* */ symbols, which are ignored by the compiler, but are used to inform the person reading what is going on. Take notice of them!

Let’s go over this program in a little more depth.

The line static char input[2048]; declares a global array of 2048 characters. This is a reserved block of data we can access anywhere from our program. In it we are going to store the user input which is typed into the command line. The static keyword makes this variable local to this file, and the [2048] section is what declares the size.

We write an infinite loop using while (1). In a conditional block 1 always evaluates to true. Therefore commands inside this loop will run forever.

To output our prompt we use the function fputs. This is a slight variation on puts which does not append a newline character. We use the fgets function for getting user input from the command line. Both of these functions require some file to write to, or read from. For this we supply the special variables stdin and stdout. These are declared in <stdio.h> and are special file variables representing input to, and output from, the command line. When passed this variable the fgets function will wait for a user to input a line of text, and when it has it will store it into the input buffer, including the newline character. So that fgets does not read in too much data we also must also supply the size of the buffer 2048.

To echo the message back to the user we use the function printf. This is a function that provides a way of printing messages consisting of several elements. It matches arguments to patterns in the given string. For example in our case we can see the %s pattern in the given string. This means that it will be replaced by whatever argument is passed in next, interpreted as a string.

For more information on these different patterns please see the documentation on printf.

How am I meant to know about functions like fgets and printf?

It isn’t immediately obvious how to know about these standard functions, and when to use them. When faced with a problem it takes experience to know when it has been solved for you by library functions.

Luckily C has a very small standard library and almost all of it can be learnt in practice. If you want to do something that seems quite basic, or fundamental, it is worth looking at the reference documentation for the standard library and checking if there are any functions included that do what you want.