Detect if stdin is a terminal or pipe?

Use isatty:

#include <stdio.h>
#include <io.h>
...    
if (isatty(fileno(stdin)))
    printf( "stdin is a terminal\n" );
else
    printf( "stdin is a file or a pipe\n");

(On windows they’re prefixed with underscores: _isatty, _fileno)

Leave a Comment