Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is isatty() in C for?

Tags:

c

linux

Hi can anyone tell me what is the paramter of isatty() in c. I have following code, but I don't understand the first output three number would be 1 and all the left is 0.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
        for(int i=0;i<100;i++){
                int t=isatty(i);
                printf("%d",t);
        }
        return 0;
}
like image 748
Hao9000 Avatar asked Mar 28 '16 07:03

Hao9000


1 Answers

A quick look at your man pages would reveal:

int isatty(int fildes);

DESCRIPTION
     The isatty() function tests whether  fildes,  an  open  file
     descriptor, is associated with a terminal device.

Further investigation would lead you to the discovery that file descriptors 0, 1 and 2 (aka STDIN_FILENO, STDOUT_FILENO and STDERR_FILENO) are by convention set up to point to your terminal when your program is running from a terminal.

like image 107
Tom Tanner Avatar answered Oct 01 '22 13:10

Tom Tanner