8

I am a bit confused regarding the implementation of stdin. What exactly is it? Is it a pointer? I tried to print the size of stdin using sizeof on a 64 bit machine and got 8. I even de-referenced it using *stdin in the %s specifier and got the not used characters in the input stream( all of them). But if I compare its de referenced value inside if I get an error. I know its an input stream. But how is it implemented?

Here is a sample:

#include<stdio.h>
#include<stdlib.h>

int main(){
    
    char a[10];

    fgets(a,10,stdin);

    printf("a: %s",a);
    
    printf("\nstdin: %s",*stdin);

//if(*stdin=="foo"){
//    printf("True");
//}//Error: invalid operands to binary == (have 'FILE' and 'char *')//and casting to char * doesn't help

    printf("\nSize of stdin: %d\n",sizeof(stdin));

    if(stdin!=NULL){
        printf("This statement is always reached");//always printed even in when input is bigger than 10 chars and otherwise
    }
    

if(!feof(stdin)){
    printf("\nThis statement is also always reached");
}
}

When I input

foobar1234567890

I get the result:

a: foobar123

stdin: 4567890

Size of stdin: 8

This statement is always reached

This statement is also always reached

And when I input

foobar

I get the output

a: foobar

stdin:

Size of stdin: 4

This statement is always reached

This statement is also always reached

I understand that stdin is kind of a FILE pointer, but I don't clearly get it. Could anyone explain the above outputs?

Community
  • 1
  • 1
Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
  • `stdin`: Standard input, for input from the terminal/console. – cs95 Jun 07 '17 at 22:39
  • "But how is it implemented", too broad, this is implementation behavior. So you are asking to made an answer for all existing compiler. Choice one compiler in your tags (gcc, clang, msvs, ...). – Stargateur Jun 07 '17 at 22:40
  • stdin is a c runtime file handle, typically this is a pointer. It is wired up automatically to the 'file' stnadard input, typically file 1 as far as the OS is concerned – pm100 Jun 07 '17 at 22:41
  • 1
    find the definition of stdin in stdio.h. Look how its implemented – pm100 Jun 07 '17 at 22:42
  • @Stargateur okay, narrowed it – Pushan Gupta Jun 07 '17 at 22:42
  • It's a handle. Handle is something you get when perform some functions. And then you give it as a parameter to other functions to change it's state. List of those functions has been given to you in one answer. This is everything you need to know to use it. If you are curious about the implementation, fetch sources of some libc implementation and dig through them. – ArturFH Jun 07 '17 at 22:44
  • Stdin is a file handle to standard input, just like stdout and stderr are handles to standard output and standard error. They have no meaningful value as integers, pointers, or anything else other than file handles. It's not a pointer, you don't use it for math or dereference it or do anything else except read input from it. Implementation of it is meaningless unless you're writing an operating system, and by the time you're doing that you should understand quite well what it is and what it's for. – Ken White Jun 07 '17 at 22:44
  • "Some kind of FILE pointer" - yes, that's exactly what it is... Hence why it's pointer-sized and you can use it with functions that take FILE pointers. – user253751 Jun 08 '17 at 01:17

5 Answers5

13

stdin is a pointer of type FILE *. The standard does not restrict the implementation beyond this, the details of what FILE is is entirely up to your compiler. It could even be an incomplete type (opaque).

Of course, trying to print a FILE with %s causes undefined behaviour (unless FILE is a typedef for char * or similar, which it almost certainly is not).

M.M
  • 138,810
  • 21
  • 208
  • 365
5

stdin (short for standard input) is a pointer to a FILE type that can only be manipulated with these functions:

https://en.wikipedia.org/wiki/C_file_input/output

On most (if not all) implementations, the FILE type is a structure with a integer file descriptor and a character buffer, allowing for buffered I/O operations. This file descriptor is opened in read-only mode and is normally connected to either the terminal, a pipe or a file.

There's also stdout (standard output) and stderr (standard error), opened in writing mode.

Ricardo Branco
  • 5,740
  • 1
  • 21
  • 31
1

It's an abstraction of the Standard Input service the OS supports. It is modeled as a stream. Check GNU LibC definitions.

J.Guarin
  • 91
  • 1
  • 3
1

stdin,stdout,stderr are 3 opened files for you app to use as the default input file, output file and error out put file.

And those 3 file have open by the system before your app start. And the files description are 1, 2 and 3.

If you close(1), the stdin file will be closed. And then, if you open another file, the stdin will be point to the new file.

All input APIs(scanf, get, ...) without FD are read data from input file, and output APIs without FD are put the value into stdout.

So, if you close(1), and open a new file with return value is 1, the printf will put values into your file.

Jimmy
  • 223
  • 1
  • 11
1
typedef struct _IO_FILE FILE;
extern struct _IO_FILE *stdin;

So it is a pointer to FILE.

The Standard says:

7.21 Input/output

stderr
stdin
stdout
which are expressions of type ‘‘pointer to FILE’’ that point to the FILE objects
associated, respectively, with the standard error, input, and output streams.
alinsoar
  • 15,386
  • 4
  • 57
  • 74
  • Now that's pretty sure its a pointer to FILE. But then how does feof always return true in this case? The accepted answer to this question https://stackoverflow.com/questions/26948723/checking-the-stdin-buffer-if-its-empty doesn't seem to work many times. But your efforts are appreciated. – Pushan Gupta Jun 08 '17 at 07:07