Jump to content

C Programming/Simple input

From Wikibooks, open books for an open world
(Redirected from C Programming/Simple Input and Output)

Now that we can create variables, let's learn how to fill those variables with input from the user. Like the output functions from Simple output, these input functions are declared in stdio.h, part of the C standard library.

Input using scanf()

[edit | edit source]

The scanf() function is the input method equivalent to the printf() output function. In its simplest invocation, the scanf format string holds a single placeholder representing the type of value that will be entered by the user. These placeholders are mostly the same as the printf() function - %d for integers, %f for floats, and %lf for doubles.

There is, however, one variation to scanf() as compared to printf(). The scanf() function requires the memory address of the variable to which you want to save the input value. While pointers (variables storing memory addresses) can be used here, this is a concept that won't be approached until later in the text. Instead, the simple technique is to use the address-of operator, &. For now it may be best to consider this "magic" before we discuss pointers.

A typical application might be like this:

Example
#include <stdio.h>  int main(void) {  int a;   printf("Please input an integer value: ");  scanf("%d", &a);  printf("You entered: %d\n", a);   return 0; } 

If you were to describe the effect of the scanf() function call above, it might read as: "Read in an integer from the user and store it at the address of variable a".

Reading strings with fgets()

[edit | edit source]

To read strings, use a different function, fgets(), like this:

Example
#include <stdio.h>  int main(void) {  char name[40];   printf("Please input your name: ");  fgets(name, sizeof(name), stdin);  printf("You entered: %s\n", name);   return 0; } 

fgets() has three parameters: a string to read into (name), the maximum size of the string (sizeof(name)), and a stream to read from (stdin).

Note Don't worry about streams for now—they will be covered in a later chapter. For now, know that stdin refers to a stream of characters the user is inputting to your program.

We give fgets() a maximum size so that, if the user types more characters than there is room for in the string, the input doesn't go past the end of name in memory and overwrite unrelated data. This is a buffer overflow. Thankfully, because we set the maximum size correctly, this doesn't happen, and any additional characters instead stay in the stream, waiting to be read by future calls to fgets() or scanf().

The size we give to fgets() is a maximum size, not a guaranteed size. fgets() stops reading when the user presses ↵ Enter.

Warning

The example above could also be accomplished with scanf("%39s", name). However, this is discouraged because:

  • You can't use sizeof; the size must be literal.
  • Due to how strings work, the size here is different from the size above—39 versus 40. This difference is easy to forget, and getting it wrong can lead to subtle buffer overflow errors that can be difficult to reproduce. More on this in a later chapter.

Pre-C11 versions of the C standard had featured gets(), which only took a string with no accompanying size. There was no way to use it safely; any size of input could be waiting in the stream, and there is no way to know in advance!