RedstoneAGX
RedstoneAGX

Reputation: 23

Can you get input from devices aside from the keyboard in C's standard library?

I was reading a book from 1997 that teaches how to program in C, and it always uses the word “usually” when specifying that functions like scanf take input from the keyboard. Because of this, I'm curious as to if functions like scanf can take input from other devices, or if it used to.

Upvotes: 2

Views: 173

Answers (2)

chux
chux

Reputation: 153508

After freopen( ..., ...., stdin), scanf() input can come from many possible sources.

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 180286

Because of this, I'm curious as to if functions like scanf can take input from other devices, or if it used to.

scanf takes input from the program's standard input. What this is connected to is a matter of the operating environment and the way the program is launched. (Look up "I/O redirection"). It is not unusual for a program's standard input to be connected to a file on disk or to the output of another program. It sometimes is connected to a socket. More rarely, it is connected to a serial port, or to a null device, or a source of zeroes or random bytes.

Historically, it might have been connected to a card or paper tape reader.

In principle, it can be connected to any device that produces data -- a mouse, for example -- but just because something is possible doesn't make it useful.

Upvotes: 4

Related Questions