With csh, tcsh, zsh or recent versions of bash, try
gcc hello.c |& tee file.txt
where
- |& instruct the shell to redirect standard error to standard output.
In other Bourne-like shells:
gcc hello.c 2>&1 | tee file.txt
In rc-like shells:
gcc hello.c >[2=1] | tee file.txt
In the fish shell:
gcc hello.c ^&1 | tee file.txt