Hub Nexus
Updated

AuthorNo author yetClaim it

See something to improve? Propose a change.

Support

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()

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:

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()

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

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).

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.

Where this page came from

This page was imported from Wikibooks. From “C programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

In these publicationsC programming

LanguagesEnglish

Licence: CC BY-SA 4.0 · Adapted from en.wikibooks.org

1

0

0

0

Spinner Logo

Comments

Spinner Logo
Version: 2CC0 1.0 — public domain
The runaway star that left the Tarantula Nebula
Version: 2CC0 1.0 — public domain
The Blackwell School, where segregation had no law behind it
Version: 2CC0 1.0 — public domain
The Eagle Nebula, seen in the infrared
Version: 2CC0 1.0 — public domain
The house where the Equal Rights Amendment was written
Version: 2CC0 1.0 — public domain
The Aleutians, the forgotten front of the Second World War
Version: 2CC0 1.0 — public domain
The Cosmic Cliffs are not cliffs