Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is stdin in C language?

Tags:

c

I want to build my own scanf function. Basic idea is data from a memory address and save it to another memory address.

What is stdin? Is it a memory-address like 000ffaa? If it is a memory-address what is it so I can build my own scanf function. Thanks!.

like image 210
The Mr. Totardo Avatar asked Jun 09 '16 07:06

The Mr. Totardo


People also ask

What is the use of stdin in C?

The stdin is the short form of the “standard input”, in C programming the term “stdin” is used for the inputs which are taken from the keyboard either by the user or from a file. The “stdin” is also known as the pointer because the developers access the data from the users or files and can perform an action on them.

What does stdin mean?

The standard input device, also referred to as stdin , is the device from which input to the system is taken. Typically this is the keyboard, but you can specify that input is to come from a serial port or a disk file, for example.

What is the use of stdin and stdout in C?

function reads the input from the standard input stream stdin and scans that input according to the format provided. The int printf(const char *format, ...) function writes the output to the standard output stream stdout and produces the output according to the format provided.

What number is stdin in C?

Standard input value stdin. Its value is 0.


Video Answer


2 Answers

No, stdin is not "a memory address".

It's an I/O stream, basically an operating-system level abstraction that allows data to be read (or written, in the case of stdout).

You need to use the proper stream-oriented I/O functions to read from the stream.

Of course you can read from RAM too, so it's best to write your own function to require a function that reads a character, then you can adapt that function to either read from RAM or from stdin.

Something like:

int my_scanf(int (*getchar_callback)(void *state), void *state, const char *fmt, ...);

Is usually reasonable. The state pointer is some user-defined state that is required by the getchar_callback() function, and passed to it by my_scanf().

like image 149
unwind Avatar answered Sep 30 '22 15:09

unwind


stdin is an "input stream", which is an abstract term for something that takes input from the user or from a file. It is an abstraction layer sitting on top of the actual file handling and I/O. The purpose of streams is mainly to make your code portable between different systems.

Reading/writing to memory is much more low-level and has nothing to do with streams as such. In order to use a stream in a meaningful way, you would have to know how a certain compiler implements the stream internally, which may not be public information. In some cases, like in Windows, streams are defined by the OS itself and can get accessed through API calls.

If you are looking to build your own scanf function, you would have to look into specific API functions for a specific OS, then build your own abstraction layer on top of those.

like image 36
Lundin Avatar answered Sep 30 '22 15:09

Lundin