Reputation: 3825
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?
Upvotes: 11
Views: 66022
Reputation: 15783
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.
Upvotes: 2
Reputation: 6079
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.
Upvotes: 5
Reputation: 223
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.
Upvotes: 1
Reputation: 141534
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).
Upvotes: 16
Reputation: 91
It's an abstraction of the Standard Input service the OS supports. It is modeled as a stream. Check GNU LibC definitions.
Upvotes: 1