Skip to content
Hub Nexus

No author yetClaim it

Version 1Updated 2 min read

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.

Get each new page of

C programming

Following means one email when a page is added, nothing when a page changes.

Following means one email when a page is added, nothing when a page changes.

Comments

Spinner Logo

Next in C programming

CC BY-SA 4.0
Its type depends on its form and value. The types of constants are character constants (e.g. ' ' is a space), integer constants (e.g. 2), floating-point constants (e.g.
CC BY-SA 4.0
C programming, Arrays and strings
CC BY-SA 4.0
C programming, Program flow control
CC BY-SA 4.0
In C programming, all executable code resides within a **function**. Note that other programming languages may distinguish between a "function", "subroutine", "subprogram", "procedure", or "method" -- in C, these are all functions. Functions are a fundamental feature of any high level programming la
CC BY-SA 4.0
A library in C is a collection of header files, exposed for use by other programs. The library therefore consists of an interface expressed in a .h file (named the "header") and an implementation expressed in a .c file. This .c file might be precompiled or otherwise inaccessible, or it might be avai
CC BY-SA 4.0
Why doesn't it work? ### Data types - List at least three data types in C On your computer, how much memory does each require? - Which ones can be used in place of another? Why?