clinux

What is isatty() in C for?


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;
}

Solution

  • 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.